YOLOX icon indicating copy to clipboard operation
YOLOX copied to clipboard

Can you share documentation for using torch hub?

Open kadirnar opened this issue 3 years ago • 6 comments

Hi @FateScript ,

I wrote the model loading code, but can you share the documentation so I can write the prediction code?

Example Yolov5: https://docs.ultralytics.com/tutorials/pytorch-hub/

import cv2
import torch
from PIL import Image

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

# Images
for f in ['zidane.jpg', 'bus.jpg']:
    torch.hub.download_url_to_file('https://ultralytics.com/images/' + f, f)  # download 2 images
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

I need this to integrate the yolox model into the SAHI library.

kadirnar avatar Aug 02 '22 22:08 kadirnar

Hi @FateScript ,

Does yolox not support the torch_hub library?

kadirnar avatar Aug 06 '22 11:08 kadirnar

Hi @FateScript ,

Does yolox not support the torch_hub library?

It should support such feature. Any issue raised? Could you please paste your error log?

FateScript avatar Aug 07 '22 04:08 FateScript

Hi @FateScript ,

I need the source code so I can write prediction code. I can't see code in hubconf.py file. Codes of other yolo models:

Yolov7 Yolov5

My Code: It gives an error on line 4.

model = torch.hub.load("Megvii-BaseDetection/YOLOX", "yolox_s")
image = torch.from_numpy(cv2.imread('data/highway1.jpg'))
image = image.permute(2, 0, 1).unsqueeze(0).float()
prediction = model(image)

Error Message:

  File ".../.cache\torch\hub\Megvii-BaseDetection_YOLOX_main\yolox\models\yolox.py", line 33, in forward
    assert targets is not None
AssertionError

How can I write these codes without source code or documentation? What should I do to use the device parameter? Can you help with this?

kadirnar avatar Aug 07 '22 08:08 kadirnar

assert targets is not None

Plz add the following code.

model.eval()

FateScript avatar Aug 08 '22 03:08 FateScript

Thank you. After applying postprocces I get 'None'. How can I fix? Code:

import torch 
import cv2
from yolox.utils import  postprocess

model = torch.hub.load("Megvii-BaseDetection/YOLOX", "yolox_s")
image = torch.from_numpy(cv2.imread('data/highway1.jpg'))
image = image.permute(2, 0, 1).unsqueeze(0).float()
prediction = model.eval()(image)
outputs = postprocess(prediction, num_classes=80, conf_thre=0.7, nms_thre=0.45, class_agnostic=False)
print(outputs)

Output:

None

kadirnar avatar Aug 08 '22 10:08 kadirnar

How can I fix?

Not sure what's in your picture. From code your posted, I believe that you might need to cv2.resize image to size 640(this value is provided in README) before using network to predict. I also notice that you are using high conf_threshold 0.7, if you really want to filter conf value greater than 0.7, you might need to use value 0.7**2 = 0.49 here.

code to refer: https://github.com/Megvii-BaseDetection/YOLOX/blob/main/tools/demo.py#L133-L184

FateScript avatar Aug 09 '22 03:08 FateScript

How can I fix?

Not sure what's in your picture. From code your posted, I believe that you might need to cv2.resize image to size 640(this value is provided in README) before using network to predict. I also notice that you are using high conf_threshold 0.7, if you really want to filter conf value greater than 0.7, you might need to use value 0.7**2 = 0.49 here.

code to refer: https://github.com/Megvii-BaseDetection/YOLOX/blob/main/tools/demo.py#L133-L184

Thank you. I ran the yolox model using the hub library. I want to change the image_size value. https://github.com/Megvii-BaseDetection/YOLOX/blob/main/yolox/models/build.py#L60

exp.input_size= input_size

Can we add this? I would like to open a PR if you agree to this change.

kadirnar avatar Aug 12 '22 20:08 kadirnar

In my opinion, pre-trained yolox should resize image to 640 to maintain the consistency of training process and eval process. I'm not sure what the true purpose of changing it. @kadirnar

FateScript avatar Aug 14 '22 00:08 FateScript

For satellite images, the img_size value must be large. That's why I wanted to give a manual value. Image

I changed the image_size parameter using the preproc function. Thank you for the repo. I forced it while coding pip and torch.hub. It will be much more comfortable if you write documentation for them.

kadirnar avatar Aug 14 '22 04:08 kadirnar