How to combine (results) of multiple pytorch models?

Hello pytorch-community,

in pytorch (& yolov5) i was able to train two models (one detects cars, the other detects bikes).

I am loading two different models (last.pt files) with the following commands:

model_c = torch.hub.load('ultralytics/yolov5', 'custom', path='/last.pt', force_reload=True)

model_b = torch.hub.load('ultralytics/yolov5', 'custom', path='/last.pt', force_reload=True)

I use an image for the model to detect cars or bikes in:

img = os.path.join('data', 'images', 'traffic.jpg')

The results command can show the model detection for cars:

results = model_c(img)

results.print()

or bikes:

results = model_b(img)

results.print()

Question 1:

How can the results of cars and bikes detections be shown in the same image at the same time?

Question 2:

Is there a way to combine two model pt.files into one?

Thank you very much in advance.

It is better if you train a new model which predicts the two classes at the same time. As for your questions, they can be solved in the following manner.

  1. Viewing the results of cars and bike detections in same image at the same time.
    You can first pass the image through model 1 and detect cars, and then pass the output image through the second model, this will detect the bikes.
  2. I have never seen if there is a way to combine the two model files in to one. A much better option would be to train a new network which performs both the classifications at the same time.
1 Like