ultralytics / yolov3

YOLOv3 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
10.22k stars 3.45k forks source link

Problems with multi batch #1766

Closed cemicel closed 3 years ago

cemicel commented 3 years ago

Hi, thanks for the project !

I slightly upgraded the detect.py code to be able to inference multi batch images, but after the update I'm getting weird bbox coordinates for 2 or more frames. This is the code snap:

`

Run inference

    t0 = time.time()
    img = torch.zeros((1, 3, self.imgsz, self.imgsz), device=self.device)
    _ = self.model(
        img.half() if self.half else img.float()) if self.device_type.type != 'cpu' else None  # run once

    with torch.no_grad():

        frames_orig = frames
        frame_shapes = [f.shape for f in frames_orig]
        frames = [letterbox(i, new_shape=self.imgsz)[0] for i in frames ]
        frames = gen_batch(frames, self.batch_size) # here I have a list of lists with np frames

        preds = []
        for img0, shapes in zip(frames, frame_shapes):

            img = np.stack(img0)

            if len(img.shape) == 3:
                img = img[:, :, ::-1].transpose(2, 0, 1)
            else:
                img = img[:, :, ::-1].transpose(0, 3, 1, 2)

            img = np.ascontiguousarray(img)

            img = torch.from_numpy(img).to(self.device)

            img = img.half() if self.half else img.float()  # uint8 to fp16/32
            img /= 255.0  # 0 - 255 to 0.0 - 1.0
            if img.ndimension() == 3:
                img = img.unsqueeze(0)

            # Inference
            t1 = torch_utils.time_synchronized()
            pred = self.model(img, augment=False)[0]
            t2 = torch_utils.time_synchronized()
            print('yolo inference time:', t2 - t1)

            # to float
            if self.half:
                pred = pred.float()

            pred = non_max_suppression(pred, self.conf_thres, self.iou_thres,multi_label=False, classes=self.classes, agnostic=self.agnostic_nms)

            for i, det in enumerate(pred):
                if det is not None and len(det):
                    # Rescale boxes from imgsz to im0 size
                    det[:, :4] = scale_coords(img.shape[2:], det[:, :4], shapes).round()

`

Is it possible to run a simple inference inference with YOLO3 model ? If yes could you tell me where I've made a mistake please.

github-actions[bot] commented 3 years ago

πŸ‘‹ Hello @cemicel, thank you for your interest in YOLOv3 πŸš€! 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

YOLOv3 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 YOLOv3 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv3 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

@cemicel see YOLOv3/5 PyTorch Hub tutorial for batched inference examples:

import torch

# Model
model = torch.hub.load('ultralytics/yolov3', 'yolov3')  # or 'yolov3_spp', 'yolov3_tiny'

# Images
imgs = ['https://ultralytics.com/images/zidane.jpg', 'https://ultralytics.com/images/bus.jpg']  # batch of images

# Inference
results = model(imgs)
results.print()  # or .show(), .save()

YOLOv5 Tutorials

cemicel commented 3 years ago

@glenn-jocher Hi thanks for the response. Yes, I saw that there is yolo5 which supports batching. I was curious why yolo3 fails when I pass batched images. Anyway, thanks for the answer, I'm using yolo5 now.

glenn-jocher commented 12 months ago

@cemicel you're welcome! It's great to hear that you've found a solution with YOLOv5. The community and the Ultralytics team have put a lot of effort into making it a versatile and powerful tool. If you have any more questions or need further assistance, feel free to ask.