I found the Issue also reported in the YOLOv7 repo (creator of Scaled-YOLOv4 also made YOLOv7).
It seems they introduced the same error in YOLOv7. This comment notes how to fix it:
The fix is to convert to long the "gain" of the gridspace on declaration.
In all lines with
gain = torch.ones(7, device=targets.device)
change to
gain = torch.ones(7, device=targets.device).long()
For some reason newest versions of cuda/torch do not do this cast automatically when needed
gain = torch.ones(7, device=targets.device)
is located here utils/general.py#L512
*** try updating utils/general.py#L512 to read:
gain = torch.ones(7, device=targets.device).long()
and utils/general.py#L1101 to read:
targets.append([i, cls, float(x.cpu()),
float(y.cpu()),
float(w.cpu()),
float(h.cpu()),
float(conf.cpu())])
^^ To do this, open the notebook in Colab, then “Save a Copy in Drive”
-
Ensure your Runtime is set to GPU:
-
Run the first cell:
# clone Scaled_YOLOv4
!git clone https://github.com/roboflow-ai/ScaledYOLOv4.git # clone repo
%cd /content/ScaledYOLOv4/
#checkout the yolov4-large branch
!git checkout yolov4-large
-
Edit
ScaledYOLOv4/utils/general.py
by navigating to it in your Colab directory, and double-clicking ongeneral.py to open it
:
-
Edit Line 512 to this:
gain = torch.ones(7, device=targets.device).long() # normalized to gridspace gain
and Edit Line 1101 to this:
targets.append([i, cls, float(x.cpu()),
float(y.cpu()),
float(w.cpu()),
float(h.cpu()),
float(conf.cpu())])
- original Line 1101
-
CTRL+s
to save your changes -
Begin running the cells the rest of the notebook, starting with this cell:
import torch
print('Using torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))
-
Be sure you generate your dataset version, ensure Resize is to
416x416
(default input size for the model, and offering the fastest inference speeds) – then export your dataset in YOLOv5 PyTorch TXT format, and copy/paste your code snippet in the cell highlighted in my screenshot, below (you can skip the cell just above it):
-
Run the training cell, and it works! (at least it did for me, just now):