yolov5 | YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite | Machine Learning library

 by   ultralytics Python Version: v7.0 License: GPL-3.0

kandi X-RAY | yolov5 Summary

yolov5 is a Python library typically used in Artificial Intelligence, Machine Learning, Deep Learning, Pytorch, Tensorflow applications. yolov5 has no bugs, it has no vulnerabilities, it has build file available, it has a Strong Copyleft License and it has medium support. You can install using 'pip install yolov5' or download it from GitHub, PyPI.
YOLOv5 is a family of object detection architectures and models pretrained on the COCO dataset, and represents Ultralytics open-source research into future vision AI methods, incorporating lessons learned and best practices evolved over thousands of hours of research and development. See the YOLOv5 Docs for full documentation on training, testing and deployment. Clone repo and install requirements.txt in a Python>=3.7.0 environment, including PyTorch>=1.7. Inference with YOLOv5 and PyTorch Hub . Models download automatically from the latest YOLOv5 release. detect.py runs inference on a variety of sources, downloading models automatically from the latest YOLOv5 release and saving results to runs/detect. The commands below reproduce YOLOv5 COCO results. Models and datasets download automatically from the latest YOLOv5 release. Training times for YOLOv5n/s/m/l/x are 1/2/4/6/8 days on a V100 GPU (Multi-GPU times faster). Use the largest --batch-size possible, or pass --batch-size -1 for YOLOv5 AutoBatch. Batch sizes shown for V100-16GB. Get started in seconds with our verified environments. Click each icon below for details. Automatically track and visualize all your YOLOv5 training runs in the cloud with Weights & Biases. Label and export your custom datasets directly to YOLOv5 for training with Roboflow.
    Support
      Quality
        Security
          License
            Reuse
            Support
              Quality
                Security
                  License
                    Reuse

                      kandi-support Support

                        summary
                        yolov5 has a medium active ecosystem.
                        summary
                        It has 36849 star(s) with 13305 fork(s). There are 348 watchers for this library.
                        summary
                        There were 1 major release(s) in the last 6 months.
                        summary
                        There are 215 open issues and 7561 have been closed. On average issues are closed in 30 days. There are 61 open pull requests and 0 closed requests.
                        summary
                        It has a neutral sentiment in the developer community.
                        summary
                        The latest version of yolov5 is v7.0
                        yolov5 Support
                          Best in #Machine Learning
                            Average in #Machine Learning
                            yolov5 Support
                              Best in #Machine Learning
                                Average in #Machine Learning

                                  kandi-Quality Quality

                                    summary
                                    yolov5 has 0 bugs and 0 code smells.
                                    yolov5 Quality
                                      Best in #Machine Learning
                                        Average in #Machine Learning
                                        yolov5 Quality
                                          Best in #Machine Learning
                                            Average in #Machine Learning

                                              kandi-Security Security

                                                summary
                                                yolov5 has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
                                                summary
                                                yolov5 code analysis shows 0 unresolved vulnerabilities.
                                                summary
                                                There are 0 security hotspots that need review.
                                                yolov5 Security
                                                  Best in #Machine Learning
                                                    Average in #Machine Learning
                                                    yolov5 Security
                                                      Best in #Machine Learning
                                                        Average in #Machine Learning

                                                          kandi-License License

                                                            summary
                                                            yolov5 is licensed under the GPL-3.0 License. This license is Strong Copyleft.
                                                            summary
                                                            Strong Copyleft licenses enforce sharing, and you can use them when creating open source projects.
                                                            yolov5 License
                                                              Best in #Machine Learning
                                                                Average in #Machine Learning
                                                                yolov5 License
                                                                  Best in #Machine Learning
                                                                    Average in #Machine Learning

                                                                      kandi-Reuse Reuse

                                                                        summary
                                                                        yolov5 releases are available to install and integrate.
                                                                        summary
                                                                        Deployable package is available in PyPI.
                                                                        summary
                                                                        Build file is available. You can build the component from source.
                                                                        summary
                                                                        Installation instructions are not available. Examples and code snippets are available.
                                                                        summary
                                                                        yolov5 saves you 1539 person hours of effort in developing the same functionality from scratch.
                                                                        summary
                                                                        It has 6133 lines of code, 412 functions and 33 files.
                                                                        summary
                                                                        It has high code complexity. Code complexity directly impacts maintainability of the code.
                                                                        yolov5 Reuse
                                                                          Best in #Machine Learning
                                                                            Average in #Machine Learning
                                                                            yolov5 Reuse
                                                                              Best in #Machine Learning
                                                                                Average in #Machine Learning
                                                                                  Top functions reviewed by kandi - BETA
                                                                                  kandi has reviewed yolov5 and discovered the below as its top functions. This is intended to give you an instant insight into yolov5 implemented functionality, and help decide if they suit your requirements.
                                                                                  • Train the model .
                                                                                    • Load dataset statistics .
                                                                                      • Calculate k - mean anchor anchors .
                                                                                        • Non - Suppression Estimation
                                                                                          • Generate a random perspective .
                                                                                            • Loads a mosaic tile .
                                                                                              • Initialize wandb .
                                                                                                • Computes the precision for each class .
                                                                                                  • Helper function to plot images .
                                                                                                    • Build targets for the given parameters .
                                                                                                      Get all kandi verified functions for this library.
                                                                                                      Get all kandi verified functions for this library.

                                                                                                      yolov5 Key Features

                                                                                                      Train Custom Data  🚀 RECOMMENDED
                                                                                                      Tips for Best Training Results  ☘️ RECOMMENDED
                                                                                                      Weights & Biases Logging  🌟 NEW
                                                                                                      Roboflow for Datasets, Labeling, and Active Learning  🌟 NEW
                                                                                                      Multi-GPU Training
                                                                                                      PyTorch Hub  ⭐ NEW
                                                                                                      TFLite, ONNX, CoreML, TensorRT Export 🚀
                                                                                                      Test-Time Augmentation (TTA)
                                                                                                      Model Ensembling
                                                                                                      Model Pruning/Sparsity
                                                                                                      Hyperparameter Evolution
                                                                                                      Transfer Learning with Frozen Layers  ⭐ NEW
                                                                                                      TensorRT Deployment
                                                                                                      COCO AP val denotes mAP@0.5:0.95 metric measured on the 5000-image COCO val2017 dataset over various inference sizes from 256 to 1536.
                                                                                                      GPU Speed measures average inference time per image on COCO val2017 dataset using a AWS p3.2xlarge V100 instance at batch-size 32.
                                                                                                      EfficientDet data from google/automl at batch size 8.
                                                                                                      Reproduce by python val.py --task study --data coco.yaml --iou 0.7 --weights yolov5n6.pt yolov5s6.pt yolov5m6.pt yolov5l6.pt yolov5x6.pt

                                                                                                      yolov5 Examples and Code Snippets

                                                                                                      3. YOLOv5模型预测:
                                                                                                      Pythondot imgLines of Code : 124dot imgLicense : Permissive (MIT)
                                                                                                      copy iconCopy
                                                                                                      
                                                                                                                                          import torch import numpy as np from models.experimental import attempt_load from utils.general import non_max_suppression, scale_coords, letterbox from utils.torch_utils import select_device import cv2 from random import randint class Detector(object): def __init__(self): self.img_size = 640 self.threshold = 0.4 self.max_frame = 160 self.init_model() def init_model(self): self.weights = 'weights/yolov5m.pt' self.device = '0' if torch.cuda.is_available() else 'cpu' self.device = select_device(self.device) model = attempt_load(self.weights, map_location=self.device) model.to(self.device).eval() model.half() # torch.save(model, 'test.pt') self.m = model self.names = model.module.names if hasattr( model, 'module') else model.names self.colors = [ (randint(0, 255), randint(0, 255), randint(0, 255)) for _ in self.names ] def preprocess(self, img): img0 = img.copy() img = letterbox(img, new_shape=self.img_size)[0] img = img[:, :, ::-1].transpose(2, 0, 1) img = np.ascontiguousarray(img) img = torch.from_numpy(img).to(self.device) img = img.half() # 半精度 img /= 255.0 # 图像归一化 if img.ndimension() == 3: img = img.unsqueeze(0) return img0, img def plot_bboxes(self, image, bboxes, line_thickness=None): tl = line_thickness or round( 0.002 * (image.shape[0] + image.shape[1]) / 2) + 1 # line/font thickness for (x1, y1, x2, y2, cls_id, conf) in bboxes: color = self.colors[self.names.index(cls_id)] c1, c2 = (x1, y1), (x2, y2) cv2.rectangle(image, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize( cls_id, 0, fontScale=tl / 3, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(image, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(image, '{} ID-{:.2f}'.format(cls_id, conf), (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) return image def detect(self, im): im0, img = self.preprocess(im) pred = self.m(img, augment=False)[0] pred = pred.float() pred = non_max_suppression(pred, self.threshold, 0.3) pred_boxes = [] image_info = {} count = 0 for det in pred: if det is not None and len(det): det[:, :4] = scale_coords( img.shape[2:], det[:, :4], im0.shape).round() for *x, conf, cls_id in det: lbl = self.names[int(cls_id)] x1, y1 = int(x[0]), int(x[1]) x2, y2 = int(x[2]), int(x[3]) pred_boxes.append( (x1, y1, x2, y2, lbl, conf)) count += 1 key = '{}-{:02}'.format(lbl, count) image_info[key] = ['{}×{}'.format( x2-x1, y2-y1), np.round(float(conf), 3)] im = self.plot_bboxes(im, pred_boxes) return im, image_info
                                                                                                      import os def pre_process(data_path): file_name = os.path.split(data_path)[1].split('.')[0] return data_path, file_name
                                                                                                      import cv2 def predict(dataset, model, ext): global img_y x = dataset[0].replace('\\', '/') file_name = dataset[1] print(x) print(file_name) x = cv2.imread(x) img_y, image_info = model.detect(x) cv2.imwrite('./tmp/draw/{}.{}'.format(file_name, ext), img_y) return image_info
                                                                                                      from core import process, predict def c_main(path, model, ext): image_data = process.pre_process(path) image_info = predict.predict(image_data, model, ext) return image_data[1] + '.' + ext, image_info if __name__ == '__main__': pass
                                                                                                      yolov5数据集制作,3.重构数据集
                                                                                                      Pythondot imgLines of Code : 43dot imgno licencesLicense : No License
                                                                                                      copy iconCopy
                                                                                                      
                                                                                                                                          # 小数据建议:copy 大数据建议:move for i in range(len(img_txt_cg_train)): shutil.copy(fimg+str(img_txt_cg_train[i]),new_dataset_train) shutil.copy(flable+str(label_txt_cg_train[i]),new_dataset_trainl) for j in range(len(img_txt_cg_test)): shutil.copy(fimg+str(img_txt_cg_test[j]),new_dataset_test) shutil.copy(flable+str(label_txt_cg_test[j]),new_dataset_testl) for q in range(len(img_txt_cg_valid)): shutil.copy(fimg+str(img_txt_cg_valid[q]),new_dataset_valid) shutil.copy(flable+str(label_txt_cg_valid[q]),new_dataset_validl)
                                                                                                      dataset │ ├─Annotations │ train_29635.xml │ train_29641.xml │ train_30090.xml │ ... │ ├─ImageSets │ └─Main │ train.txt │ test.txt │ valid.txt │ img_train.txt │ img_test.txt │ img_valid.txt │ ├─data │ ├─train │ ├─test │ └─valid │ ├─JPEGImages │ train_29635.jpg │ train_29641.jpg │ train_30090.jpg │ ... │ └─worktxt train_29635.txt train_29641.txt train_30090.txt ...
                                                                                                      yolov5数据集制作,2.数据集划分训练、测试、验证
                                                                                                      Pythondot imgLines of Code : 28dot imgno licencesLicense : No License
                                                                                                      copy iconCopy
                                                                                                      
                                                                                                                                          dataset │ ├─Annotations │ train_29635.xml │ train_29641.xml │ train_30090.xml │ ... │ ├─ImageSets │ └─Main │ train.txt │ test.txt │ valid.txt │ img_train.txt │ img_test.txt │ img_valid.txt │ ├─JPEGImages │ train_29635.jpg │ train_29641.jpg │ train_30090.jpg │ ... │ └─worktxt train_29635.txt train_29641.txt train_30090.txt ...
                                                                                                      Unable to find module/folder despite being in the same directory
                                                                                                      Pythondot imgLines of Code : 5dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                                      copy iconCopy
                                                                                                      from pathlib import Path
                                                                                                      import sys
                                                                                                      
                                                                                                      sys.path.append(str(Path(__file__, "..", "targetTrack", "yolov5").resolve()))
                                                                                                      
                                                                                                      How to load custom model in pytorch
                                                                                                      Pythondot imgLines of Code : 2dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                                      copy iconCopy
                                                                                                      model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp/weights/last.pt', force_reload=True) 
                                                                                                      
                                                                                                      How to read text file in python after that write something on same file
                                                                                                      Pythondot imgLines of Code : 12dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                                      copy iconCopy
                                                                                                      with open('result.txt','r') as file:
                                                                                                          data = file.readlines() # return list of all the lines in the text file.
                                                                                                      
                                                                                                      for a in range(len(data)): # loop through all the lines.
                                                                                                          if 'person' in data[a] and 'tv' in data[a]: # Check if person and tv in the same line.
                                                                                                              data[a] = data[a].replace('\n','') + ' status : High\n'
                                                                                                          elif 'person' in data[a] and 'laptop' in data[a]:# Check if person and laptop in the same line.
                                                                                                              data[a] = data[a] + ' status : Low\n'
                                                                                                      
                                                                                                      with open('result.txt','w') as file: 
                                                                                                          file.writelines(data) # write lines to the file.
                                                                                                      
                                                                                                      How to use yolov5 api with flask offline?
                                                                                                      Pythondot imgLines of Code : 4dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                                      copy iconCopy
                                                                                                      # model = torch.hub.load("ultralytics/yolov5", "yolov5s", force_reload=True)  # force_reload to recache
                                                                                                      
                                                                                                      model = torch.hub.load(r'C:\Users\Milan\Projects\yolov5', 'custom', path=r'C:\Users\Milan\Projects\yolov5\models\yolov5s.pt', source='local')
                                                                                                      
                                                                                                      Class label order in YoloV5 using Ultralytics code
                                                                                                      Pythondot imgLines of Code : 6dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                                      copy iconCopy
                                                                                                      nc: 3
                                                                                                      classes: ['cat', 'dog', 'car']
                                                                                                      
                                                                                                      0 0.156 0.321 0.254 0.198
                                                                                                      2 0.574 0.687 0.115 0.301
                                                                                                      
                                                                                                      OpenCV Window Freezing when trying to combine with Neural Network Image Classification
                                                                                                      Pythondot imgLines of Code : 3dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                                      copy iconCopy
                                                                                                      cv2.imshow("window", img)
                                                                                                      cv2.waitKey(1)  # perform GUI housekeeping tasks
                                                                                                      
                                                                                                      PyTorch - save only positive pictures where pedestrian are detected
                                                                                                      Pythondot imgLines of Code : 3dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                                      copy iconCopy
                                                                                                      if 0 in results.pandas().xyxy[0]['class']:
                                                                                                        results.save()
                                                                                                      
                                                                                                      Community Discussions

                                                                                                      Trending Discussions on yolov5

                                                                                                      How to read text file in python after that write something on same file
                                                                                                      chevron right
                                                                                                      YOLOv5 get boxes, scores, classes, nums
                                                                                                      chevron right
                                                                                                      TypeError: Can't convert vector element for 'scores', index=0
                                                                                                      chevron right
                                                                                                      How to use pt file
                                                                                                      chevron right
                                                                                                      How to place the dataset for training Yolov5?
                                                                                                      chevron right
                                                                                                      Yolov5 object detection training
                                                                                                      chevron right
                                                                                                      OpenCV Window Freezing when trying to combine with Neural Network Image Classification
                                                                                                      chevron right
                                                                                                      How to change the bounding box thickness and label text in yolov5?
                                                                                                      chevron right
                                                                                                      How to use yolov5 api with flask offline?
                                                                                                      chevron right
                                                                                                      Deploying Yolov5 to azure
                                                                                                      chevron right

                                                                                                      QUESTION

                                                                                                      How to read text file in python after that write something on same file
                                                                                                      Asked 2022-Apr-14 at 07:53

                                                                                                      i am currently working with yolov5 code is here i slightly modified in code to save results in text file called output.txt

                                                                                                      output.txt file :

                                                                                                      0: 480x640 2 persons, 1 tv, 1: 480x640 5 persons, 1 cell phone, Done. (0.759s) Mon, 04 April 11:39:48
                                                                                                      0: 480x640 2 persons, 1 laptop, 1: 480x640 4 persons, 1 oven, Done. (0.763s) Mon, 04 April 11:39:50
                                                                                                      

                                                                                                      After that , i wanna validate text file line by line with following conditions

                                                                                                      if person and tv detected in same row add status : High 
                                                                                                      if person and laptop detected in same row add status : Low 
                                                                                                      

                                                                                                      Expected output :

                                                                                                      0: 480x640 2 persons, 1 tv, 1: 480x640 5 persons, 1 cell phone, Done. (0.759s) Mon, 04 April 11:39:48 status : High 
                                                                                                      0: 480x640 2 persons, 1 laptop, 1: 480x640 4 persons, 1 oven, Done. (0.763s) Mon, 04 April 11:39:50 status : Low
                                                                                                      

                                                                                                      Is it possible if yes how can i resolve that

                                                                                                      Thanks in advance

                                                                                                      ANSWER

                                                                                                      Answered 2022-Apr-14 at 07:17

                                                                                                      This code may helps you.

                                                                                                      with open('result.txt','r') as file:
                                                                                                          data = file.readlines() # return list of all the lines in the text file.
                                                                                                      
                                                                                                      for a in range(len(data)): # loop through all the lines.
                                                                                                          if 'person' in data[a] and 'tv' in data[a]: # Check if person and tv in the same line.
                                                                                                              data[a] = data[a].replace('\n','') + ' status : High\n'
                                                                                                          elif 'person' in data[a] and 'laptop' in data[a]:# Check if person and laptop in the same line.
                                                                                                              data[a] = data[a] + ' status : Low\n'
                                                                                                      
                                                                                                      with open('result.txt','w') as file: 
                                                                                                          file.writelines(data) # write lines to the file.
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/71867691

                                                                                                      QUESTION

                                                                                                      YOLOv5 get boxes, scores, classes, nums
                                                                                                      Asked 2022-Apr-05 at 08:26

                                                                                                      im trying to bind the Object Tracking with Deep Sort in my Project and i need to get the boxes, scores, classes, nums.

                                                                                                      Loading Pretrained Yolov5 model:

                                                                                                      model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
                                                                                                      model.eval()
                                                                                                      

                                                                                                      Getting the Prediction:

                                                                                                      result = model(img)
                                                                                                      print(result.shape)
                                                                                                      print(result)
                                                                                                      torch.Size([8, 6])
                                                                                                      tensor([[277.50000, 379.25000, 410.50000, 478.75000,   0.90625,   2.00000],
                                                                                                              [404.00000, 205.12500, 498.50000, 296.00000,   0.88623,   2.00000],
                                                                                                              [262.50000, 247.75000, 359.50000, 350.25000,   0.88281,   2.00000],
                                                                                                              [210.50000, 177.75000, 295.00000, 261.75000,   0.83154,   2.00000],
                                                                                                              [195.50000, 152.50000, 257.75000, 226.00000,   0.78223,   2.00000],
                                                                                                              [137.00000, 146.75000, 168.00000, 162.00000,   0.55713,   2.00000],
                                                                                                              [ 96.00000, 130.12500, 132.50000, 161.12500,   0.54199,   2.00000],
                                                                                                              [ 43.56250,  89.56250,  87.68750, 161.50000,   0.50146,   5.00000]], device='cuda:0')
                                                                                                      tensor([[277.50000, 379.25000, 410.50000, 478.75000,   0.90625,   2.00000],
                                                                                                              [404.00000, 205.12500, 498.50000, 296.00000,   0.88623,   2.00000],
                                                                                                              [262.50000, 247.75000, 359.50000, 350.25000,   0.88281,   2.00000],
                                                                                                              [210.50000, 177.75000, 295.00000, 261.75000,   0.83154,   2.00000],
                                                                                                              [195.50000, 152.50000, 257.75000, 226.00000,   0.78223,   2.00000],
                                                                                                              [137.00000, 146.75000, 168.00000, 162.00000,   0.55713,   2.00000],
                                                                                                              [ 96.00000, 130.12500, 132.50000, 161.12500,   0.54199,   2.00000],
                                                                                                              [ 43.56250,  89.56250,  87.68750, 161.50000,   0.50146,   5.00000]], device='cuda:0')
                                                                                                      

                                                                                                      so now my question is how do i get the boxes, scores, classes, nums in each variables? I need that for the Object Tracking

                                                                                                      I tried it once with the example on Pytorch Documentation: result.xyxy[0]

                                                                                                      but in my Case I get an Error:

                                                                                                      Tensor has no attribute xyxy
                                                                                                      

                                                                                                      ANSWER

                                                                                                      Answered 2022-Apr-05 at 08:26

                                                                                                      The output from the model is a torch tensor and has no xyxy method. You need to extract the values manually. Either you can go through each detection one by one:

                                                                                                      import torch
                                                                                                      
                                                                                                      det = torch.rand(8, 6)
                                                                                                      
                                                                                                      for *xyxy, conf, cls in det:
                                                                                                          print(*xyxy)
                                                                                                          print(conf)
                                                                                                          print(cls)
                                                                                                      

                                                                                                      or you can slice the detections tensor by:

                                                                                                      xyxy = det[:, 0:4]
                                                                                                      conf = det[:, 4]
                                                                                                      cls = det[:, 5]
                                                                                                      
                                                                                                      print(xyxy)
                                                                                                      print(conf)
                                                                                                      print(cls)
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/71737788

                                                                                                      QUESTION

                                                                                                      TypeError: Can't convert vector element for 'scores', index=0
                                                                                                      Asked 2022-Apr-04 at 21:29

                                                                                                      I'm trying to take the output of a yolov5s.onnx model and and run NMSBoxes on it. But I keep getting this error:

                                                                                                      Traceback (most recent call last):
                                                                                                        File "python_detection.py", line 132, in 
                                                                                                          class_ids, confidences, boxes = wrap_detection(inputImage, outs[0])
                                                                                                        File "python_detection.py", line 88, in wrap_detection
                                                                                                          indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
                                                                                                      TypeError: Can't convert vector element for 'scores', index=0
                                                                                                      

                                                                                                      Everywhere I look, people are using the exact same code as me. Which makes sense, since this code was mostly copied from a tutorial. So I don't know what I'm doing so wrong that keeps giving me this error.

                                                                                                      Here's the full function:

                                                                                                      def wrap_detection(input_image, output_data):
                                                                                                          class_ids = []
                                                                                                          confidences = []
                                                                                                          boxes = []
                                                                                                      
                                                                                                          rows = output_data.shape[0]
                                                                                                      
                                                                                                          image_width, image_height, _ = input_image.shape
                                                                                                      
                                                                                                          x_factor = image_width / INPUT_WIDTH
                                                                                                          y_factor =  image_height / INPUT_HEIGHT
                                                                                                      
                                                                                                          for r in range(rows):
                                                                                                              row = output_data[r]
                                                                                                              confidence = row[4]
                                                                                                              if confidence >= 0.4:
                                                                                                      
                                                                                                                  classes_scores = row[5:]
                                                                                                                  _, _, _, max_indx = cv2.minMaxLoc(classes_scores)
                                                                                                                  class_id = max_indx[1]
                                                                                                                  if (classes_scores[class_id] > .25):
                                                                                                      
                                                                                                                      confidences.append(confidence)
                                                                                                      
                                                                                                                      class_ids.append(class_id)
                                                                                                      
                                                                                                                      x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item()
                                                                                                                      left = int((x - 0.5 * w) * x_factor)
                                                                                                                      top = int((y - 0.5 * h) * y_factor)
                                                                                                                      width = int(w * x_factor)
                                                                                                                      height = int(h * y_factor)
                                                                                                                      box = np.array([left, top, width, height])
                                                                                                                      boxes.append(box)
                                                                                                      
                                                                                                          '''
                                                                                                          Print the raw output
                                                                                                          '''
                                                                                                          # Save output
                                                                                                          np.set_printoptions(threshold=sys.maxsize)
                                                                                                          file = open("python_raw_model_output.txt", "w+")
                                                                                                          for i in range(len(boxes)):
                                                                                                              file.write(str(boxes[i]) + " " + str(confidences[i]) + " " + str(class_ids[i]))
                                                                                                              file.write("\n")
                                                                                                          file.close()
                                                                                                      
                                                                                                          # NMS on the lists
                                                                                                          indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
                                                                                                      
                                                                                                          result_class_ids = []
                                                                                                          result_confidences = []
                                                                                                          result_boxes = []
                                                                                                      
                                                                                                          for i in indexes:
                                                                                                              result_confidences.append(confidences[i])
                                                                                                              result_class_ids.append(class_ids[i])
                                                                                                              result_boxes.append(boxes[i])
                                                                                                      
                                                                                                          return result_class_ids, result_confidences, result_boxes
                                                                                                      

                                                                                                      ANSWER

                                                                                                      Answered 2022-Apr-04 at 21:29

                                                                                                      I had the same issue. It seemed to be related to the cuda configuration as it works fine on the cpu. I did not figure out exactly what was wrong but I worked around the issue by using fastNMS: enter link description here

                                                                                                      Source https://stackoverflow.com/questions/71739616

                                                                                                      QUESTION

                                                                                                      How to use pt file
                                                                                                      Asked 2022-Apr-01 at 19:25

                                                                                                      I'm trying to make a currency recognition model and I did so using a dataset on kaggle and colab using yolov5 and I exactly carried out the steps explained on yolov5 github. At the end, I downloaded a .pt file which has the weights of the model and now I want to use it in python file to detect and recognize currency . How to do this?

                                                                                                      I am a beginner in computer vision and I am totally confused about what to do. I am searching over and over but I don't reach anything.

                                                                                                      import torch
                                                                                                      
                                                                                                      # Model
                                                                                                      model=torch.load('E:\_best.pt')
                                                                                                      
                                                                                                      # Images
                                                                                                      imgs=['E:\Study\currency.jpg']
                                                                                                      
                                                                                                      # Inference
                                                                                                      results = model(imgs)
                                                                                                      
                                                                                                      # Results
                                                                                                      results.print()
                                                                                                      results.save()  # or .show()
                                                                                                      results.show()
                                                                                                      results.xyxy[0]  # img1 predictions (tensor)
                                                                                                      results.pandas().xyxy[0]
                                                                                                      

                                                                                                      ANSWER

                                                                                                      Answered 2022-Apr-01 at 17:19

                                                                                                      If you want to read trained parameters from .pt file and load it into your model, you could do the following.

                                                                                                      file = "model.pt"
                                                                                                      model = your_model()
                                                                                                      model.load_state_dict(torch.load(file))
                                                                                                      # this will automatically load the file and load the parameters into the model.
                                                                                                      
                                                                                                      

                                                                                                      before calling load_state_dict(), be sure that the .pt file contains only model parameters, otherwise, error occurs. This can be checked by print(torch.load(file)).

                                                                                                      Source https://stackoverflow.com/questions/71707006

                                                                                                      QUESTION

                                                                                                      How to place the dataset for training Yolov5?
                                                                                                      Asked 2022-Mar-25 at 14:06

                                                                                                      I’m currently working on object detection using yolov5. I trained a model with a custom dataset which has 3 classes = [‘Car’,‘Motorcycle’,‘Person’]

                                                                                                      I have many questions related to yolov5.

                                                                                                      All the custom images are labelled using Roboflow.

                                                                                                      question1 : As you can see from the table that my dataset has mix of images with different sizes. Will this be a problem in training? And also assume that i’ve trained the model and got ‘best.pt’. Will that model work efficiently in any dimensions of images/videos.

                                                                                                      question 2:

                                                                                                      Is this directory model correct for training. Even i have ‘test’ directory but it seems that the directory is not at all used. The images in the ‘test’ folder is useless. ( I know that i’m asking dumb questions, please bare with me.)

                                                                                                      Is it ok if place all my images like this

                                                                                                      And should i need a ‘test’ folder?

                                                                                                      question3: What is the ‘imgsz’ in detect.py? Is it downsampling the input source?

                                                                                                      I’ve spent more than 3 weeks in yolo. I love it but i find some parts difficult to grasp. kindly provide suggestion for this questions. Thanks in advance.

                                                                                                      ANSWER

                                                                                                      Answered 2022-Mar-25 at 14:06

                                                                                                      "question1 : As you can see from the table that my dataset has mix of images with different sizes. Will this be a problem in training? And also assume that i’ve trained the model and got ‘best.pt’. Will that model work efficiently in any dimensions of images/videos."

                                                                                                      • As long as you've resized/normalized all of your images to be the same square size, then you should be fine. YOLO trains on square images. You can use a platform like Roboflow to process your images so they not only come out in the right structure (for your images and annotation files) but also resize them while generating your dataset so they are all the same size. http://roboflow.com/ - you just need to make a public workspace to upload your images to and you can use the platform free. Here's a video that covers custom training with YOLOv5: https://www.youtube.com/watch?v=x0ThXHbtqCQ

                                                                                                      Roboflow's python package can also be used to extract your images programmatically: https://docs.roboflow.com/python

                                                                                                      "Is this directory model correct for training. Even i have ‘test’ directory but it seems that the directory is not at all used. The images in the ‘test’ folder is useless. ( I know that i’m asking dumb questions, please bare with me.)"

                                                                                                      • Yes that directory model is correct from training. Its what I have whenever I run YOLOv5 training too.

                                                                                                      You do need a test folder if you want to run inference against the test folder images to learn more about your model's performance.

                                                                                                      The 'imgsz' parameter in detect.py is for setting the height/width of the images for inference. You set it at the value you used for --img when you ran train.py.

                                                                                                      For example: Resized images to 640 by 640 when generating your images for training? Use (640, 640) for the 'imgsz' parameter (that is the default value). And that would also mean you set --img to 640 when you ran train.py

                                                                                                      YOLOv5's Github: Tips for Best Training Results https://github.com/ultralytics/yolov5/wiki/Tips-for-Best-Training-Results

                                                                                                      Roboflow's Model Production Tips: https://docs.roboflow.com/model-tips

                                                                                                      Source https://stackoverflow.com/questions/71612175

                                                                                                      QUESTION

                                                                                                      Yolov5 object detection training
                                                                                                      Asked 2022-Mar-25 at 04:06

                                                                                                      Please i need you help concerning my yolov5 training process for object detection!

                                                                                                      I try to train my object detection model yolov5 for detecting small object ( scratch). For labelling my images i used roboflow, where i applied some data augmentation and some pre-processing that roboflow offers as a services. when i finish the pre-processing step and the data augmentation roboflow gives the choice for different output format, in my case it is yolov5 pytorch, and roboflow does everything for me splitting the data into training validation and test. Hence, Everything was set up as it should be for my data preparation and i got at the end the folder with data.yaml and the images with its labels, in data.yaml i put the path of my training and validation sets as i saw in the GitHub tutorial for yolov5. I followed the steps very carefully tought.

                                                                                                      The problem is when the training start i get nan in the obj and box column as you can see in the picture bellow, that i don't know the reason why, can someone relate to that or give me any clue to find the solution please, it's my first project in computer vision.

                                                                                                      This is what i get when the training process starts

                                                                                                      This the last message error when the training finish

                                                                                                      I think the problem comes maybe from here but i don't know how to fix it, i used the code of yolov5 team as it's in the tuto

                                                                                                      The training continue without any problem but the map and precision remains 0 all the process !!

                                                                                                      Ps : Here is the link of tuto i followed : https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data

                                                                                                      ANSWER

                                                                                                      Answered 2021-Dec-04 at 09:38

                                                                                                      Running my code in colab worked successfully and the resulats were good. I think that the problem was in my personnel laptop environment maybe the version of pytorch i was using '1.10.0+cu113', or something else ! If you have any advices to set up my environnement for yolov5 properly i would be happy to take from you guys. many Thanks again to @alexheat

                                                                                                      Source https://stackoverflow.com/questions/70199621

                                                                                                      QUESTION

                                                                                                      OpenCV Window Freezing when trying to combine with Neural Network Image Classification
                                                                                                      Asked 2022-Mar-15 at 15:33

                                                                                                      I am using a NN to detect 4 types of objects (chassis, front-spoiler, hubcap, wheel) in the live feed of my webcam. When one is detected, I want to display an image with information about it (chassis.png, front-spoiler.png, hubcap.png, wheel.png). When I run my NN and hold one of the items in front of the webcam, the opencv windows freezes and doesnt display anything. What is the reason for that?

                                                                                                      def displayImg(path):
                                                                                                          img = cv2.imread(path)
                                                                                                          cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
                                                                                                          cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
                                                                                                          cv2.imshow("window", img)
                                                                                                      
                                                                                                      # ----------------LIVE DETECTIONS ---------------
                                                                                                      imagePath = "picture.jpg"
                                                                                                      frontSpoilerImageOpen = False
                                                                                                      chassisImageOpen = False
                                                                                                      hubcapImageOpen = False
                                                                                                      wheelImageOpen = False
                                                                                                      
                                                                                                      model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp5/weights/last.pt', force_reload=True)
                                                                                                      cap = cv2.VideoCapture(0)
                                                                                                      while cap.isOpened():
                                                                                                          ret, frame = cap.read()
                                                                                                          results = model(frame)
                                                                                                      
                                                                                                          try:
                                                                                                              detectedItem = results.pandas().xyxy[0].iloc[0, 6]
                                                                                                              if detectedItem == "front-spoiler" and not frontSpoilerImageOpen:
                                                                                                      
                                                                                                                  frontSpoilerImageOpen = False
                                                                                                                  chassisImageOpen = False
                                                                                                                  hubcapImageOpen = False
                                                                                                                  wheelImageOpen = False
                                                                                                      
                                                                                                                  displayImg(os.path.join("imagesToDisplay", "front-spoiler.png"))
                                                                                                                  frontSpoilerImageOpen = True
                                                                                                      
                                                                                                              elif detectedItem == "chassis" and not chassisImageOpen:
                                                                                                      
                                                                                                                  frontSpoilerImageOpen = False
                                                                                                                  chassisImageOpen = False
                                                                                                                  hubcapImageOpen = False
                                                                                                                  wheelImageOpen = False
                                                                                                      
                                                                                                                  displayImg(os.path.join("imagesToDisplay", "chassis.png"))
                                                                                                                  chassisImageOpen = True
                                                                                                      
                                                                                                              elif detectedItem == "hubcap" and not hubcapImageOpen:
                                                                                                      
                                                                                                                  frontSpoilerImageOpen = False
                                                                                                                  chassisImageOpen = False
                                                                                                                  hubcapImageOpen = False
                                                                                                                  wheelImageOpen = False
                                                                                                      
                                                                                                                  displayImg(os.path.join("imagesToDisplay", "hubcap.png"))
                                                                                                                  hubcapImageOpen = True
                                                                                                      
                                                                                                      
                                                                                                              elif detectedItem == "wheel" and not wheelImageOpen:
                                                                                                      
                                                                                                                  frontSpoilerImageOpen = False
                                                                                                                  chassisImageOpen = False
                                                                                                                  hubcapImageOpen = False
                                                                                                                  wheelImageOpen = False
                                                                                                      
                                                                                                                  displayImg(os.path.join("imagesToDisplay", "wheel.png"))
                                                                                                                  wheelImageOpen = True
                                                                                                          except Exception as e:
                                                                                                              print(e)
                                                                                                      

                                                                                                      ANSWER

                                                                                                      Answered 2022-Mar-15 at 15:33

                                                                                                      I had a similar issue. Adding cv2.waitKey after cv2.imshow helped in my case:

                                                                                                      cv2.imshow("window", img)
                                                                                                      cv2.waitKey(1)  # perform GUI housekeeping tasks
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/71478801

                                                                                                      QUESTION

                                                                                                      How to change the bounding box thickness and label text in yolov5?
                                                                                                      Asked 2022-Mar-15 at 08:55

                                                                                                      I am using YOLOv5s for object detection on custom datasets, there are multiple objects in given video, sometimes label text and bounding box thickness looks very bad. how can I customize these things?

                                                                                                      ANSWER

                                                                                                      Answered 2022-Mar-15 at 08:55

                                                                                                      when using detect.py, pass in the following arguments to adjust the labels and bounding boxes:

                                                                                                      --line-thickness 1
                                                                                                      --hide-labels True
                                                                                                      --hide-conf True
                                                                                                      

                                                                                                      For the --line-thickness argument, pass in an integer value to adjust the thickness, for labels and confidence, they are set to False by default. Setting them to True will hide them.

                                                                                                      Source https://stackoverflow.com/questions/71390000

                                                                                                      QUESTION

                                                                                                      How to use yolov5 api with flask offline?
                                                                                                      Asked 2022-Feb-28 at 18:45

                                                                                                      I was able to run the Flask app with yolov5 on a PC with an internet connection. I followed the steps mentioned in yolov5 docs and used this file: yolov5/utils/flask_rest_api/restapi.py,

                                                                                                      But I need to achieve the same offline(On a particular PC). Now the issue is, when I am using the following:

                                                                                                      model = torch.hub.load("ultralytics/yolov5", "yolov5", force_reload=True)
                                                                                                      

                                                                                                      It tries to download model from internet. And throws an error.

                                                                                                      Urllib.error.URLError: 
                                                                                                      

                                                                                                      How to get the same results offline.

                                                                                                      Thanks in advance.

                                                                                                      ANSWER

                                                                                                      Answered 2022-Feb-25 at 15:38

                                                                                                      If you want to run detection offline, you need to have the model already downloaded.

                                                                                                      So, download the model (for example yolov5s.pt) from https://github.com/ultralytics/yolov5/releases and store it for example to the yolov5/models.

                                                                                                      After that, replace

                                                                                                      # model = torch.hub.load("ultralytics/yolov5", "yolov5s", force_reload=True)  # force_reload to recache
                                                                                                      

                                                                                                      with

                                                                                                      model = torch.hub.load(r'C:\Users\Milan\Projects\yolov5', 'custom', path=r'C:\Users\Milan\Projects\yolov5\models\yolov5s.pt', source='local')
                                                                                                      

                                                                                                      With this line, you can run detection also offline.

                                                                                                      Note: When you start the app for the first time with the updated torch.hub.load, it will download the model if not present (so you do not need to download it from https://github.com/ultralytics/yolov5/releases).

                                                                                                      Source https://stackoverflow.com/questions/71251177

                                                                                                      QUESTION

                                                                                                      Deploying Yolov5 to azure
                                                                                                      Asked 2022-Feb-25 at 09:17

                                                                                                      How to deploy Custom trained YOLOV5 model to azure using azure functions? I couldn’t find any online resources

                                                                                                      Complete Scenario:

                                                                                                      There is a sharepoint app where user will upload the videos, once the new video is uploaded, it should trigger the flow to azure function, this azure function should be able to predict the objects in the frame with the custom trained yolov5 model

                                                                                                      ANSWER

                                                                                                      Answered 2022-Feb-25 at 09:17

                                                                                                      We are not sure about the Deployment of YOLO5 in Azure Function.

                                                                                                      Follow the below steps, it will work for any ML model using Azure Function

                                                                                                      Prerequisites:
                                                                                                      1. Install Azure CLI
                                                                                                      2. Install Azure Function Core Tools
                                                                                                      1. Create and test Azure function locally

                                                                                                      Using CLI create a python function

                                                                                                      _# Create and activate an environment_  
                                                                                                      python3 -m venv .venv  
                                                                                                      source .venv/bin/activate_
                                                                                                      # Create a FunctionApp Project Locally_  
                                                                                                      func init --worker-runtime python_
                                                                                                      # Create a Function_  
                                                                                                      func new --name  --template "HTTP trigger" --authlevel anonymous
                                                                                                      

                                                                                                      Edit the__init__.py file for your business logic to modify your model.

                                                                                                      Add the required packages in requirement.txt. After that install the packages using

                                                                                                      pip install -r requirements.txt
                                                                                                      

                                                                                                      Test your function locally. using func start

                                                                                                      2. Create the Required Resources for your Project on Azure 3. Deploy the function to Azure

                                                                                                      Deploy local project code to the Function App created on Azure, using

                                                                                                      func azure functionapp publish ****
                                                                                                      

                                                                                                      Source https://stackoverflow.com/questions/71246199

                                                                                                      Community Discussions, Code Snippets contain sources that include Stack Exchange Network

                                                                                                      Vulnerabilities

                                                                                                      No vulnerabilities reported

                                                                                                      Install yolov5

                                                                                                      You can install using 'pip install yolov5' or download it from GitHub, PyPI.
                                                                                                      You can use yolov5 like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.

                                                                                                      Support

                                                                                                      For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .
                                                                                                      Find more information at:
                                                                                                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
                                                                                                      Find more libraries
                                                                                                      Explore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits​
                                                                                                      Save this library and start creating your kit

                                                                                                      Share this Page

                                                                                                      share link

                                                                                                      Consider Popular Machine Learning Libraries

                                                                                                      tensorflow

                                                                                                      by tensorflow

                                                                                                      youtube-dl

                                                                                                      by ytdl-org

                                                                                                      models

                                                                                                      by tensorflow

                                                                                                      pytorch

                                                                                                      by pytorch

                                                                                                      keras

                                                                                                      by keras-team

                                                                                                      Try Top Libraries by ultralytics

                                                                                                      yolov3

                                                                                                      by ultralyticsPython

                                                                                                      ultralytics

                                                                                                      by ultralyticsPython

                                                                                                      JSON2YOLO

                                                                                                      by ultralyticsPython

                                                                                                      xview-yolov3

                                                                                                      by ultralyticsPython

                                                                                                      flickr_scraper

                                                                                                      by ultralyticsPython

                                                                                                      Compare Machine Learning Libraries with Highest Support

                                                                                                      youtube-dl

                                                                                                      by ytdl-org

                                                                                                      scikit-learn

                                                                                                      by scikit-learn

                                                                                                      models

                                                                                                      by tensorflow

                                                                                                      tensorflow

                                                                                                      by tensorflow

                                                                                                      keras

                                                                                                      by keras-team

                                                                                                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
                                                                                                      Find more libraries
                                                                                                      Explore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits​
                                                                                                      Save this library and start creating your kit