ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.48k stars 16.29k forks source link

YOLO V5 training for custom object detection #2957

Closed Sandr001 closed 3 years ago

Sandr001 commented 3 years ago

❔Attribute Error when I run the following command :

python train.py --img 640 --batch 16 --epochs 300--data training/dataset.yaml --cfg training/yolov5l.yaml

This is what I'm getting. Please help!

(base) E:\Sandra\yolov5> python train.py --img 640 --batch 4 --epochs 5 --data training/dataset.yaml --cfg training/yolov5l.yaml
github: up to date with https://github.com/ultralytics/yolov5
YOLOv5  v5.0-42-g4200674 torch 1.8.1+cpu CPU

Namespace(adam=False, artifact_alias='latest', batch_size=4, bbox_interval=-1, bucket='', cache_images=False, cfg='training/yolov5l.yaml', data='training/dataset.yaml', device='', entity=None, epochs=3, evolve=False, exist_ok=False, global_rank=-1, hyp='data/hyp.scratch.yaml', image_weights=False, img_size=[640, 640], label_smoothing=0.0, linear_lr=False, local_rank=-1, multi_scale=False, name='exp', noautoanchor=False, nosave=False, notest=False, project='runs/train', quad=False, rect=False, resume=False, save_dir='runs\\train\\exp5', save_period=-1, single_cls=False, sync_bn=False, total_batch_size=4, upload_dataset=False, weights='yolov5s.pt', workers=8, world_size=1)
tensorboard: Start with 'tensorboard --logdir runs/train', view at http://localhost:6006/
hyperparameters: lr0=0.01, lrf=0.2, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0
wandb: Currently logged in as: sandra (use `wandb login --relogin` to force relogin)
wandb: Tracking run with wandb version 0.10.27
wandb: Syncing run exp5
wandb:  View project at https://wandb.ai/sandra/YOLOv5
wandb:  View run at https://wandb.ai/sandra/YOLOv5/runs/387eznhk
wandb: Run data is saved locally in E:\Sandra\yolov5\wandb\run-20210428_102327-387eznhk
wandb: Run `wandb offline` to turn off syncing.

Traceback (most recent call last):
  File "train.py", line 544, in <module>
    train(hyp, opt, device, tb_writer)
  File "train.py", line 72, in train
    wandb_logger = WandbLogger(opt, save_dir.stem, run_id, data_dict)
  File "E:\Sandra\yolov5\utils\wandb_logging\wandb_utils.py", line 108, in __init__
    self.data_dict = self.setup_training(opt, data_dict)
  File "E:\Sandra\yolov5\utils\wandb_logging\wandb_utils.py", line 139, in setup_training
    self.train_artifact_path, self.train_artifact = self.download_dataset_artifact(data_dict.get('train'),
AttributeError: 'str' object has no attribute 'get'

wandb: Waiting for W&B process to finish, PID 6648
wandb: Program failed with code 1.  Press ctrl-c to abort syncing.
wandb:
wandb: Find user logs for this run at: E:\Sandra\yolov5\wandb\run-20210428_102327-387eznhk\logs\debug.log
wandb: Find internal logs for this run at: E:\Sandra\yolov5\wandb\run-20210428_102327-387eznhk\logs\debug-internal.log
wandb: Synced 6 W&B file(s), 0 media file(s), 0 artifact file(s) and 0 other file(s)
wandb:
wandb: Synced exp5: https://wandb.ai/sandra/YOLOv5/runs/387eznhk
github-actions[bot] commented 3 years ago

πŸ‘‹ Hello @Sandr001, thank you for your interest in πŸš€ YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a πŸ› Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 3 years ago

@Sandr001 hi thanks for the bug report! @AyushExel we seem to have a W&B bug here. User is on Windows on the latest commit v5.0-42-g4200674.

Seems like data_dict.get('train') is failing as data_dict has no 'train' key.

glenn-jocher commented 3 years ago

@Sandr001 looking at your error message it appears that your dataset.yaml file has no 'train' field, in which case there is no bug in the code. This field is required, and is used to identify your training set. See Train Custom Data tutorial to get started:

YOLOv5 Tutorials

If you believe you have a reproducible issue, we suggest you close this issue and raise a new one using the πŸ› Bug Report template, providing screenshots and a minimum reproducible example to help us better understand and diagnose your problem. Thank you!

AyushExel commented 3 years ago

@Sandr001 as Glenn pointed out, probably your dataset.yaml file is missing some info. Can you share the file here for us to take a look at? The file should contain train and val paths

Sandr001 commented 3 years ago

Thank you Glenn and Ayush for responding to my issue. The below given is the content in my dataset.yaml file:

train:training/data/images/train val:training/data/images/valid

nc:1

names:['duck']

@Sandr001 as Glenn pointed out, probably your dataset.yaml file is missing some info. Can you share the file here for us to take a look at? The file should contains train and val paths

AyushExel commented 3 years ago

@Sandr001 Can you try adding space after the : . like:

train: training/data/images/train
val: training/data/images/valid

nc: 1
names: ['duck]
Sandr001 commented 3 years ago

@Sandr001 Can you try adding space after the : . like:

train: training/data/images/train
val: training/data/images/valid

nc: 1
names: ['duck]

Adding space after ':' solved my issue. Thanks a lot.

glenn-jocher commented 3 years ago

@Sandr001 great!

Sandr001 commented 3 years ago

Hi.. I have a doubt.. will YOLO V5 work on all versions of Ubuntu?

glenn-jocher commented 3 years ago

@Sandr001

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are passing. These tests evaluate proper operation of basic YOLOv5 functionality, including training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu.

Sandr001 commented 3 years ago

Hi.. Can I get the detected label names from the image saved into a .txt file in the detect folder like --save-txt, --save-conf etc ?

glenn-jocher commented 3 years ago

@Sandr001 not with detect.py, but if you run inference using YOLOv5 PyTorch Hub models you can use .pandas() to produce detections with class names:

import cv2
import torch
from PIL import Image

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Images
for f in ['zidane.jpg', 'bus.jpg']:  # download 2 images
    torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/releases/download/v1.0/' + f, f)
img1 = Image.open('zidane.jpg')  # PIL image
img2 = cv2.imread('bus.jpg')[:, :, ::-1]  # OpenCV image (BGR to RGB)
imgs = [img1, img2]  # batch of images

# Inference
results = model(imgs, size=640)  # includes NMS

# Results
results.print()  
results.save()  # or .show()

results.xyxy[0]  # img1 predictions (tensor)
results.pandas().xyxy[0]  # img1 predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 1  433.50  433.50   517.5  714.5    0.687988     27     tie
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

See PyTorch Hub tutorial for full details:

YOLOv5 Tutorials

Sandr001 commented 3 years ago

Thank you.

Sandr001 commented 3 years ago

@Sandr001 not with detect.py, but if you run inference using YOLOv5 PyTorch Hub models you can use .pandas() to produce detections with class names:

import cv2
import torch
from PIL import Image

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Images
for f in ['zidane.jpg', 'bus.jpg']:  # download 2 images
    torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/releases/download/v1.0/' + f, f)
img1 = Image.open('zidane.jpg')  # PIL image
img2 = cv2.imread('bus.jpg')[:, :, ::-1]  # OpenCV image (BGR to RGB)
imgs = [img1, img2]  # batch of images

# Inference
results = model(imgs, size=640)  # includes NMS

# Results
results.print()  
results.save()  # or .show()

results.xyxy[0]  # img1 predictions (tensor)
results.pandas().xyxy[0]  # img1 predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 1  433.50  433.50   517.5  714.5    0.687988     27     tie
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

See PyTorch Hub tutorial for full details:

YOLOv5 Tutorials

Hi! If it was for custom data what are the changes to be made? How to point inference to the model trained with our custom data?

glenn-jocher commented 3 years ago

@Sandr001 see PyTorch Hub tutorial for information on loading custom YOLOv5 models.

YOLOv5 Tutorials

Sandr001 commented 3 years ago

Hi.. For my doubt clarification I am asking this question. Please help with this doubt. Can YOLOv5 be used solely for classification purposes.? Since both classification and detection is done at a go, will YOLOv5 help in this for my custom data? Thank you in advance.

glenn-jocher commented 3 years ago

@Sandr001 yes, you can train a YOLOv5 classifier with this branch: https://github.com/ultralytics/yolov5/tree/classifier

python classifier.py

See argparser for details and example usage.

glenn-jocher commented 3 years ago

https://github.com/ultralytics/yolov5/blob/classifier/classifier.py

# YOLOv5 classifier training
# Usage: python classifier.py --model yolov5s --data mnist --epochs 10 --img 128
github-actions[bot] commented 3 years ago

πŸ‘‹ Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 πŸš€ resources:

Access additional Ultralytics ⚑ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 πŸš€ and Vision AI ⭐!