ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.93k stars 16.4k forks source link

Inference 'augmented' image from Pytorch Hub instead of image list #6882

Closed lannguyen0910 closed 2 years ago

lannguyen0910 commented 2 years ago

Search before asking

Question

Hi, i'm using torch.hub.load to load my custom weights. There is no problem if i use the model input as an image list, which use cv2.imread(IMAGE_PATH)[..., ::-1] to convert from the path of the image. The output after inference is:

<models.common.Detections object at 0x7ff7e759c2d0>

However, when i try to load an image in Tensor type. Because i use some augmentation techniques like testing-time augmentation (TTA). The result after inference now becomes, for example:

tensor([[[7.58108e+00, 3.29767e+00, 8.19291e+00,  ..., 1.63066e-03, 1.83365e-03, 3.13231e-03],
         [1.48919e+01, 2.81062e+00, 9.60818e+00,  ..., 1.08319e-03, 8.73214e-04, 1.84057e-03],
         [1.97235e+01, 3.86513e+00, 1.48165e+01,  ..., 7.01686e-04, 6.10586e-04, 8.99242e-04],
         ...,
         [5.62207e+02, 5.96428e+02, 1.67612e+02,  ..., 6.05156e-02, 4.12644e-02, 1.58415e-02],
         [5.92910e+02, 5.98386e+02, 1.88376e+02,  ..., 1.05861e-01, 6.31578e-02, 2.09839e-02],
         [6.30867e+02, 6.02710e+02, 1.75896e+02,  ..., 1.14207e-01, 6.17458e-02, 1.67981e-02]]], device='cuda:0')

Therefore, i cannot use some methods from https://github.com/ultralytics/yolov5/issues/36 to get the bboxes, classes, scores such as .pandas(), .to_json().

Is there any approach to solve my problem? Thank you so much in advance!

Additional

No response

github-actions[bot] commented 2 years ago

👋 Hello @lannguyen0910, 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://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

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), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 2 years ago

@lannguyen0910 AutoShape() classes act as pass-throughs for Tensor inputs: https://github.com/ultralytics/yolov5/blob/e6e36aac109794999f1dafab244b9ec4887a33d1/models/common.py#L524-L527

lannguyen0910 commented 2 years ago

@lannguyen0910 AutoShape() classes act as pass-throughs for Tensor inputs:

https://github.com/ultralytics/yolov5/blob/e6e36aac109794999f1dafab244b9ec4887a33d1/models/common.py#L524-L527

Thank you for your reply, i have changed the loading from the hub to (based on the hubconf.py module):

torch.hub.load('ultralytics/yolov5', 'custom', path=weight, autoshape=True, force_reload=True)

But the inference model still returns a Tensor, not a models.common.Detections object. Could you show me where i was missing or provide me with a more specific solution? Thank you very much!

glenn-jocher commented 2 years ago

@lannguyen0910 the only solution is not to supply a Tensor. As I said AutoShape wrappers treat Tensors as passthoughs. You can pass anything else, i.e. files, PIL images, numpy arrays, lists, etc.

lannguyen0910 commented 2 years ago

@lannguyen0910 the only solution is not to supply a Tensor. As I said AutoShape wrappers treat Tensors as passthoughs. You can pass anything else, i.e. files, PIL images, numpy arrays, lists, etc.

I misunderstood the meaning of "pass-through". Thank you very much for the clarification!