yolov7 icon indicating copy to clipboard operation
yolov7 copied to clipboard

How to train custom 'yolov7-mask.pt' ?

Open by011 opened this issue 2 years ago • 11 comments

From the "instance.ipynb" file, i found out that 'yolov7-mask.pt' is different from regular .pt weight file, may I know how to train a custom one for my own school work? Tq

by011 avatar Nov 11 '22 07:11 by011

Use train.py in the mask branch. Pro tip: you can still transfer learn from yolov7-mask.pt

awarebayes avatar Nov 11 '22 10:11 awarebayes

I may have lied to you... There is a u7 branch which is for development. It does have training code for segmentation.

I just tried running the train.py in the mask branch and the code does not appear to have sense for some reason:

Traceback (most recent call last):
  File "/home/mscherbina/Documents/github_repos/yolov7/train.py", line 363, in train
    loss, loss_items = compute_loss_ota(pred, targets.to(device), imgs)  # loss scaled by batch_size
  File "/home/mscherbina/Documents/github_repos/yolov7/utils/loss.py", line 585, in __call__
    bs, as_, gjs, gis, targets, anchors = self.build_targets(p, targets, imgs)
  File "/home/mscherbina/Documents/github_repos/yolov7/utils/loss.py", line 641, in build_targets
    indices, anch = self.find_3_positive(p, targets)
  File "/home/mscherbina/Documents/github_repos/yolov7/utils/loss.py", line 813, in find_3_positive
    gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]]  # xyxy gain
KeyError: 0

p is <class 'dict'> with keys ['mask_iou', 'attn', 'bbox_and_cls', 'bases', 'sem'] Whereas the function expects a tensor

awarebayes avatar Nov 11 '22 10:11 awarebayes

The training code for mask branch has not been released yet because of detectron2 dependecies but you can train your custom dataset for instance segmentation in the u7 branch.

afzixhaikh avatar Nov 12 '22 11:11 afzixhaikh

I may have lied to you... There is a u7 branch which is for development. It does have training code for segmentation.

I just tried running the train.py in the mask branch and the code does not appear to have sense for some reason:

Traceback (most recent call last):
  File "/home/mscherbina/Documents/github_repos/yolov7/train.py", line 363, in train
    loss, loss_items = compute_loss_ota(pred, targets.to(device), imgs)  # loss scaled by batch_size
  File "/home/mscherbina/Documents/github_repos/yolov7/utils/loss.py", line 585, in __call__
    bs, as_, gjs, gis, targets, anchors = self.build_targets(p, targets, imgs)
  File "/home/mscherbina/Documents/github_repos/yolov7/utils/loss.py", line 641, in build_targets
    indices, anch = self.find_3_positive(p, targets)
  File "/home/mscherbina/Documents/github_repos/yolov7/utils/loss.py", line 813, in find_3_positive
    gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]]  # xyxy gain
KeyError: 0

p is <class 'dict'> with keys ['mask_iou', 'attn', 'bbox_and_cls', 'bases', 'sem'] Whereas the function expects a tensor

I see...... maybe it is due to detectron2 dependencies. Btw, thank for your reply.

by011 avatar Nov 14 '22 01:11 by011

The training code for mask branch has not been released yet because of detectron2 dependecies but you can train your custom dataset for instance segmentation in the u7 branch.

Yep. But I found out the "mask" one gave slightly better segmentation than the "u7". Thx for your reply.

by011 avatar Nov 14 '22 01:11 by011

I also ran into this issue when training on the mask branch:

  ...
  File "/media/cat/oadams/yolov7/train.py", line 363, in train
    loss, loss_items = compute_loss_ota(pred, targets.to(device), imgs)  # loss scaled by batch_size
  File "/media/cat/oadams/yolov7/utils/loss.py", line 585, in __call__
    bs, as_, gjs, gis, targets, anchors = self.build_targets(p, targets, imgs)
  File "/media/cat/oadams/yolov7/utils/loss.py", line 641, in build_targets
    indices, anch = self.find_3_positive(p, targets)
  File "/media/cat/oadams/yolov7/utils/loss.py", line 811, in find_3_positive
    gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]]  # xyxy gain
KeyError: 0

Does anyone know how to resolve this or is it still not ready yet?

I've trained on the u7 branch before which seems to require training from scratch (no pretrained weights available there?) but was hoping to try the mask branch since there are the yolov7-mask.pt pre-trained weights.

oadams avatar Dec 06 '22 02:12 oadams

@oadams have you resolveed it?

lyf6 avatar Dec 27 '22 02:12 lyf6

up, i have the same problem over here

mohcineelharras avatar Dec 31 '22 08:12 mohcineelharras

If this issue was resolved, can anyone give the solution please?

ziauddin07 avatar Apr 28 '23 03:04 ziauddin07

Hi Have you resolved this issue?

LiamHO1999 avatar Jun 24 '23 10:06 LiamHO1999

Hi. Has anyone solved this issue?

siddhu003 avatar May 16 '24 09:05 siddhu003