WongKinYiu / yolov7

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
GNU General Public License v3.0
13.19k stars 4.17k forks source link

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

Open by011 opened 1 year ago

by011 commented 1 year ago

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

awarebayes commented 1 year ago

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

awarebayes commented 1 year ago

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

afzixhaikh commented 1 year ago

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.

by011 commented 1 year ago

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 commented 1 year ago

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.

oadams commented 1 year ago

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.

lyf6 commented 1 year ago

@oadams have you resolveed it?

mohcineelharras commented 1 year ago

up, i have the same problem over here

ziauddin07 commented 1 year ago

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

LiamHO1999 commented 1 year ago

Hi Have you resolved this issue?

siddhu003 commented 3 months ago

Hi. Has anyone solved this issue?