# Error: Roboflow-Custom-Scaled-YOLOv4

**URL:** <https://discuss.roboflow.com/t/error-roboflow-custom-scaled-yolov4/1920>\
**Category:** 🤝  Community Help\
**Created:** [April 13, 2023, 1:43pm UTC](https://discuss.roboflow.com/t/error-roboflow-custom-scaled-yolov4/1920 "2023-04-13T13:43:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marlene](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/marlene/32/1403_2.png) [@Marlene](https://discuss.roboflow.com/u/Marlene)\
**Post date:** [April 13, 2023, 1:43pm UTC](https://discuss.roboflow.com/t/error-roboflow-custom-scaled-yolov4/1920/1 "2023-04-13T13:43:24Z")

</div>

I am getting this error :  
Epoch gpu\_mem GIoU obj cls total targets img\_size  
0% 0/9 [00:03\<?, ?it/s]  
Traceback (most recent call last):  
File “/content/gdrive/MyDrive/Marlene\_PhD/obj\_det/ScaledYOLOv4/train.py”, line 443, in   
train(hyp, opt, device, tb\_writer)  
File “/content/gdrive/MyDrive/Marlene\_PhD/obj\_det/ScaledYOLOv4/train.py”, line 260, in train  
loss, loss\_items = compute\_loss(pred, targets.to(device), model) # scaled by batch\_size  
File “/content/gdrive/MyDrive/Marlene\_PhD/obj\_det/ScaledYOLOv4/utils/general.py”, line 446, in compute\_loss  
tcls, tbox, indices, anchors = build\_targets(p, targets, model) # targets  
File “/content/gdrive/MyDrive/Marlene\_PhD/obj\_det/ScaledYOLOv4/utils/general.py”, line 556, in build\_targets  
indices.append((b, a, gj.clamp\_(0, gain[3]), gi.clamp\_(0, gain[2]))) # image, anchor, grid indices  
**RuntimeError: result type Float can’t be cast to the desired output type long int**  
CPU times: user 161 ms, sys: 36.7 ms, total: 198 ms  
Wall time: 20.1 s

I tried changing the the torch==1.8.0+cu101 torchvision==0.9.0+cu101 from 2.0.0 but then got this error  
ImportError: /usr/local/lib/python3.9/dist-packages/mish\_cuda-0.0.3-py3.9-linux-x86\_64.egg/mish\_cuda/\_C.cpython-39-x86\_64-linux-gnu.so: undefined symbol: \_ZNK2at10TensorBase8data\_ptrIdEEPT\_v CPU times: user 9.91 ms, sys: 10.3 ms, total: 20.2 ms Wall time: 931 ms

* * *

Does anyone know what I might have got wrong, and what can be done to resolve this?

---

<div class="post-metadata">

**Author:** ![Mohamed](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/mohamed/32/62_2.png) [@Mohamed](https://discuss.roboflow.com/u/Mohamed)\
**Post date:** [April 19, 2023, 3:59am UTC](https://discuss.roboflow.com/t/error-roboflow-custom-scaled-yolov4/1920/2 "2023-04-19T03:59:17Z")

</div>

Hi @Marlene - this is an error I’ve had some trouble debugging, due to the fact the original repo also notes the Issue, and has not been updated since June 2021.  
 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/8/8a3d574c2c5a1829283a14f1abdde132afaca342.png)

I also want to add that notebook errors are reported and tracked here: [Issues · roboflow/notebooks · GitHub](https://github.com/roboflow/notebooks/issues)

Regarding the error you’re experiencing, from an initial look:

```auto
File “/content/gdrive/MyDrive/Marlene_PhD/obj_det/ScaledYOLOv4/utils/general.py”, line 556, in build_targets
indices.append((b, a, gj.clamp_(0, gain[3]), gi.clamp_(0, gain[2]))) # image, anchor, grid indices
**RuntimeError: result type Float can’t be cast to the desired output type long int**

```

It appears the error comes in here with `indices.append()`: [ScaledYOLOv4/general.py at aea215d495056132f91391f8e618682bef376338 · WongKinYiu/ScaledYOLOv4 · GitHub](https://github.com/WongKinYiu/ScaledYOLOv4/blob/aea215d495056132f91391f8e618682bef376338/utils/general.py#L556)

```auto
        # Define
        b, c = t[:, :2].long().T # image, class
        gxy = t[:, 2:4] # grid xy
        gwh = t[:, 4:6] # grid wh
        gij = (gxy - offsets).long()
        gi, gj = gij.T # grid xy indices

        # Append
        a = t[:, 6].long() # anchor indices
        indices.append((b, a, gj.clamp_(0, gain[3]), gi.clamp_(0, gain[2]))) # image, anchor, grid indices

```

- Beginning of `build_targets` function: [ScaledYOLOv4/general.py at aea215d495056132f91391f8e618682bef376338 · WongKinYiu/ScaledYOLOv4 · GitHub](https://github.com/WongKinYiu/ScaledYOLOv4/blob/aea215d495056132f91391f8e618682bef376338/utils/general.py#L556)

```auto
def build_targets(p, targets, model):
    # Build targets for compute_loss(), input targets(image,class,x,y,w,h)
    det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module
    na, nt = det.na, targets.shape[0] # number of anchors, targets
    tcls, tbox, indices, anch = [], [], [], []
    gain = torch.ones(7, device=targets.device) # normalized to gridspace gain
    ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt)
    targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices

    g = 0.5 # bias
    off = torch.tensor([[0, 0],
                        [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m
                        # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
                        ], device=targets.device).float() * g # offsets

    for i in range(det.nl):
        anchors = det.anchors[i]
        gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain

        # Match targets to anchors
        t = targets * gain
        if nt:
            # Matches
            r = t[:, :, 4:6] / anchors[:, None] # wh ratio
            j = torch.max(r, 1. / r).max(2)[0] < model.hyp['anchor_t'] # compare
            # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
            t = t[j] # filter

            # Offsets
            gxy = t[:, 2:4] # grid xy
            gxi = gain[[2, 3]] - gxy # inverse
            j, k = ((gxy % 1. < g) & (gxy > 1.)).T
            l, m = ((gxi % 1. < g) & (gxi > 1.)).T
            j = torch.stack((torch.ones_like(j), j, k, l, m))
            t = t.repeat((5, 1, 1))[j]
            offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
        else:
            t = targets[0]
            offsets = 0

        # Define
        b, c = t[:, :2].long().T # image, class
        gxy = t[:, 2:4] # grid xy
        gwh = t[:, 4:6] # grid wh
        gij = (gxy - offsets).long()
        gi, gj = gij.T # grid xy indices

        # Append
        a = t[:, 6].long() # anchor indices
        indices.append((b, a, gj.clamp_(0, gain[3]), gi.clamp_(0, gain[2]))) # image, anchor, grid indices
        tbox.append(torch.cat((gxy - gij, gwh), 1)) # box
        anch.append(anchors[a]) # anchors
        tcls.append(c) # class

    return tcls, tbox, indices, anch

```

---

<div class="post-metadata">

**Author:** ![Mohamed](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/mohamed/32/62_2.png) [@Mohamed](https://discuss.roboflow.com/u/Mohamed)\
**Post date:** [April 19, 2023, 4:55am UTC](https://discuss.roboflow.com/t/error-roboflow-custom-scaled-yolov4/1920/3 "2023-04-19T04:55:02Z")

</div>

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:

- [RuntimeError: result type Float can't be cast to the desired output type long int · Issue #35 · WongKinYiu/yolov7 · GitHub](https://github.com/WongKinYiu/yolov7/issues/35#issuecomment-1178800685)

```auto
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

```

![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/0/0d40406015768e68200168b9cc88fed9f2b3437c.png)

`gain = torch.ones(7, device=targets.device)` is located here [utils/general.py#L512](https://github.com/WongKinYiu/ScaledYOLOv4/blob/aea215d495056132f91391f8e618682bef376338/utils/general.py#L512)

\*\*\* try updating [utils/general.py#L512](https://github.com/WongKinYiu/ScaledYOLOv4/blob/aea215d495056132f91391f8e618682bef376338/utils/general.py#L512) to read:  
`gain = torch.ones(7, device=targets.device).long()`

and [utils/general.py#L1101](https://github.com/WongKinYiu/ScaledYOLOv4/blob/aea215d495056132f91391f8e618682bef376338/utils/general.py#L1101) to read:

```auto
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”  
 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/6/6de9755ff4b5a91c4bff64232d755210c9265322.png)  
 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/1/120d977eaabb1f591639deaf37b8a21488175db2.png)

1. Ensure your Runtime is set to GPU:  
 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/6/691e0a85e11951eed5d1dff172bfaa41fbec8ee2.png)

2. Run the first cell:

```auto
# 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

```

1. Edit `ScaledYOLOv4/utils/general.py` by navigating to it in your Colab directory, and double-clicking on `general.py to open it`:  

2. Edit Line 512 to this:  
`gain = torch.ones(7, device=targets.device).long() # normalized to gridspace gain`  
 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/a/ad0bb63efb6262d4c79e18cbc2a53597cece2442.png)

and Edit Line 1101 to this:

```auto
targets.append([i, cls, float(x.cpu()),   
              float(y.cpu()),   
              float(w.cpu()),   
              float(h.cpu()),   
              float(conf.cpu())])

```

- original [Line 1101](https://github.com/WongKinYiu/ScaledYOLOv4/blob/aea215d495056132f91391f8e618682bef376338/utils/general.py#L1101)

1. `CTRL+s` to save your changes

2. Begin running the cells the rest of the notebook, starting with this cell:

```auto
import torch
print('Using torch %s %s' % (torch. __version__ , torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))

```

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/8/8f0bfca21f99bff8025a07952e18ce5a83eaf324.png)

1. Be sure you generate your dataset version, ensure [Resize](https://docs.roboflow.com/image-transformations/image-preprocessing#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](https://roboflow.com/formats/yolov5-pytorch-txt), and copy/paste your code snippet in the cell highlighted in my screenshot, below (you can skip the cell just above it):  

2. **Run the training cell, and it works!** _(at least it did for me, just now)_:  

- [My shared notebook](https://colab.research.google.com/drive/1K7Ssl7U5FFk19XtGHHWgeaY0sM7F42mh?usp=sharing)

---

<div class="post-metadata">

**Author:** ![Mohamed](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/mohamed/32/62_2.png) [@Mohamed](https://discuss.roboflow.com/u/Mohamed)\
**Post date:** [April 19, 2023, 4:58am UTC](https://discuss.roboflow.com/t/error-roboflow-custom-scaled-yolov4/1920/4 "2023-04-19T04:58:16Z")

</div>

[Updated `ScaledYOLOv4/utils/general.py`](https://drive.google.com/file/d/1I0OfJ_4sFGMBWPYYf-qBL-SRIhyuRBoY/view?usp=sharing) (Google Drive download)

Once this PR is merged to the [`yolov4-large` branch of the `Scaled-YOLOv4` repo fork](https://github.com/roboflow/ScaledYOLOv4/tree/yolov4-large), you’ll be able to run the notebook while avoiding making any of the manual changes noted above ^ : [update line 512 and line 1101 of utils/general.py by mo-traor3-ai · Pull Request #2 · roboflow/ScaledYOLOv4 · GitHub](https://github.com/roboflow/ScaledYOLOv4/pull/2)

---

<div class="post-metadata">

**Author:** ![Marlene](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/marlene/32/1403_2.png) [@Marlene](https://discuss.roboflow.com/u/Marlene)\
**Post date:** [April 19, 2023, 7:31am UTC](https://discuss.roboflow.com/t/error-roboflow-custom-scaled-yolov4/1920/5 "2023-04-19T07:31:42Z")

</div>

Hi @Mohamed, thanks for your detailed reply, I will try it out and let you know how it goes.
