Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Square training and Rectangular inference? #3110

Closed
github2016-yuan opened this issue May 11, 2021 · 4 comments
Closed

Square training and Rectangular inference? #3110

github2016-yuan opened this issue May 11, 2021 · 4 comments
Labels
question Further information is requested

Comments

@github2016-yuan
Copy link

❔Question

I use this repo to train my custom dataset and it works well but I still have some questions here.

Resolution of image in my dataset is 3840 * 1080 and I use cmd like below to train: (train.py)
--img 640 --batch 16 --epochs 300 --weights yolov5s.pt

It means I use square training (image is scaled into 640 * 640 with padding) and I get best.pt and last.pt sucessfully.

Then I use cmd below to detect: (detect.py)

--source a.mp4

I get expected nice result.

It means I use square inference but I find something odd while debug in detect.py

the actual resolution of detected image is scaled to 640 * 192. That is to say I use rectangular inference here in fact.

In my opinion, training and inference should have the same resolution but here it is not.

Glad to get some advice.

@github2016-yuan github2016-yuan added the question Further information is requested label May 11, 2021
@github-actions
Copy link
Contributor

github-actions bot commented May 11, 2021

👋 Hello @github2016-yuan, 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
Copy link
Member

glenn-jocher commented May 11, 2021

@github2016-yuan 👋 Hello, thank you for asking about the differences between train.py, detect.py and test.py in YOLOv5.

These 3 files are designed for different purposes and utilize different dataloaders with different settings. train.py dataloaders are designed for a speed-accuracy compromise, test.py is designed to obtain the best mAP on a validation dataset, and detect.py is designed for best real-world inference results. A few important aspects of each:

train.py

  • trainloader LoadImagesAndLabels(): designed to load train dataset images and labels. Augmentation capable and enabled.

    yolov5/train.py

    Lines 188 to 192 in fca5e2a

    # Trainloader
    dataloader, dataset = create_dataloader(train_path, imgsz, batch_size, gs, opt,
    hyp=hyp, augment=True, cache=opt.cache_images, rect=opt.rect, rank=rank,
    world_size=opt.world_size, workers=opt.workers,
    image_weights=opt.image_weights, quad=opt.quad, prefix=colorstr('train: '))
  • testloader LoadImagesAndLabels(): designed to load val dataset images and labels. Augmentation capable but disabled.

    yolov5/train.py

    Lines 199 to 202 in fca5e2a

    testloader = create_dataloader(test_path, imgsz_test, batch_size * 2, gs, opt, # testloader
    hyp=hyp, cache=opt.cache_images and not opt.notest, rect=True, rank=-1,
    world_size=opt.world_size, workers=opt.workers,
    pad=0.5, prefix=colorstr('val: '))[0]
  • image size: 640
  • rectangular inference: False
  • confidence threshold: 0.001
  • iou threshold: 0.6
  • multi-label: True
  • padding: None

test.py

  • dataloader LoadImagesAndLabels(): designed to load train, val, test dataset images and labels. Augmentation capable but disabled.

    yolov5/test.py

    Lines 89 to 90 in fca5e2a

    dataloader = create_dataloader(data[task], imgsz, batch_size, gs, opt, pad=0.5, rect=True,
    prefix=colorstr(f'{task}: '))[0]
  • image size: 640
  • rectangular inference: True
  • confidence threshold: 0.001
  • iou threshold: 0.6
  • multi-label: True
  • padding: 0.5 * maximum stride

detect.py

  • dataloaders (multiple): designed for loading multiple types of media (images, videos, globs, directories, streams).

    yolov5/detect.py

    Lines 46 to 53 in fca5e2a

    # Set Dataloader
    vid_path, vid_writer = None, None
    if webcam:
    view_img = check_imshow()
    cudnn.benchmark = True # set True to speed up constant image size inference
    dataset = LoadStreams(source, img_size=imgsz, stride=stride)
    else:
    dataset = LoadImages(source, img_size=imgsz, stride=stride)
  • image size: 640
  • rectangular inference: True
  • confidence threshold: 0.25
  • iou threshold: 0.45
  • multi-label: False
  • padding: None

YOLOv5 PyTorch Hub

models.autoShape() class used for image loading, preprocessing, inference and NMS. For more info see YOLOv5 PyTorch Hub Tutorial

yolov5/models/common.py

Lines 225 to 250 in fca5e2a

class autoShape(nn.Module):
# input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS
conf = 0.25 # NMS confidence threshold
iou = 0.45 # NMS IoU threshold
classes = None # (optional list) filter by class
def __init__(self, model):
super(autoShape, self).__init__()
self.model = model.eval()
def autoshape(self):
print('autoShape already enabled, skipping... ') # model already converted to model.autoshape()
return self
@torch.no_grad()
@torch.cuda.amp.autocast(torch.cuda.is_available())
def forward(self, imgs, size=640, augment=False, profile=False):
# Inference from various sources. For height=640, width=1280, RGB images example inputs are:
# filename: imgs = 'data/samples/zidane.jpg'
# URI: = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/zidane.jpg'
# OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3)
# PIL: = Image.open('image.jpg') # HWC x(640,1280,3)
# numpy: = np.zeros((640,1280,3)) # HWC
# torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values)
# multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images

@fabiozappo
Copy link

@github2016-yuan this could help you understanding the benefits of rectangular inference in detect.py: ultralytics/yolov3#232

@github2016-yuan
Copy link
Author

Fine @fabiozappo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants