PyTorch_YOLOv4 icon indicating copy to clipboard operation
PyTorch_YOLOv4 copied to clipboard

mismatch results compared with official YOLOv4

Open cjnjuwhy opened this issue 3 years ago • 15 comments

I tested yolov4.cfg and yolov4.weights downloaded from AB, and test its mAP results with

CUDA_VISIBLE_DEVICES='5' python test.py --cfg ./cfg/yolov4.cfg --weights ./weights/yolov4.weights --img-size 320 --save-json --data ./data/coco.yaml

(cfg file in your repo, but weights file from https://github.com/AlexeyAB/darknet)

but only got 0.45 [email protected], how is that? Have I missed some important things? or use the weights and cfg wrongly? Thanks for your replying.

COCO results for img-size=320 7131607357279_ pic_hd

and this is for img-size=416 image

cjnjuwhy avatar Dec 07 '20 16:12 cjnjuwhy

weights file are training with different decoder in this and ab repo. this repo use new_coord=1 while ab use new_coord=0. if you want to inference weights from ab repo, you could try u3_preview branch.

WongKinYiu avatar Dec 07 '20 23:12 WongKinYiu

Hi, I test weights from DarkNet project, and still a small amount margin between yours (u3_preview) and https://github.com/AlexeyAB/darknet. Details: test2017: weights from AB, tested with u3_preview, image resolution 416x416 138491607954857_ pic_hd weights from AB, tested with u3_preview, image resolution 512x512 137681607949089_ pic

compared with original YOLOv4 in 416x416, mAP & [email protected] are 41.2 (+1.6) & 61.8 (+2.6) compared with original YOLOv4 in 512x512, mAP & [email protected] are 43.0 (+1.3) & 64.9 (+1.9)

The gaps are much smaller than that in master branch, but still non-negligible

cjnjuwhy avatar Dec 14 '20 14:12 cjnjuwhy

scale_x_y implementation is needed here.

WongKinYiu avatar Dec 15 '20 11:12 WongKinYiu

thx for your answer, but i wonder what is the implementation of scale_x_y, I'm not that familiar with YOLOv4 🙏

cjnjuwhy avatar Dec 15 '20 15:12 cjnjuwhy

scale_x_y implementation is needed here.

Hi, how to realize scale_x_y

GuoQuanhao avatar Sep 24 '21 09:09 GuoQuanhao

https://github.com/AlexeyAB/darknet/issues/3293

WongKinYiu avatar Sep 24 '21 09:09 WongKinYiu

io = p.clone()  # inference output
# io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid  # xy
io[..., :2] =  torch.sigmoid(io[..., :2]) * 1.2 - 0.5 * (1.2 - 1)
io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh  # wh yolo method
io[..., :4] *= self.stride
torch.sigmoid_(io[..., 4:])
return io.view(bs, -1, self.no), p  # view [1, 3, 13, 13, 85] as [1, 507, 85]

我这样修改的,但是精度变成0了

GuoQuanhao avatar Sep 24 '21 09:09 GuoQuanhao

xy 少了 + self.grid

WongKinYiu avatar Sep 24 '21 09:09 WongKinYiu

io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid  # xy
io[..., :2] =  torch.sigmoid(io[..., :2]) * 1.2 - 0.5 * (1.2 - 1)
io[..., :2] =  torch.sigmoid(io[..., :2]) * 1.2 - 0.5 * (1.2 - 1)
io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid  # xy

应该是哪个呢,我感到比较疑惑

GuoQuanhao avatar Sep 24 '21 09:09 GuoQuanhao

後者, 沒有加 grid, 就是所有 box 都在圖片左上角的意思.

WongKinYiu avatar Sep 24 '21 09:09 WongKinYiu

另外要注意, original yolov4 三個預測層的參數不一樣.

WongKinYiu avatar Sep 24 '21 09:09 WongKinYiu

io = p.clone()  # inference output
io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid  # xy
io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh  # wh yolo method
io[..., :4] *= self.stride
torch.sigmoid_(io[..., 4:])

我使用原版yolov4-mish在416能在val下获得0.471,但在test下只有0.399,这低于0.415

io = p.clone()  # inference output
io[..., :2] =  torch.sigmoid(io[..., :2]) * 1.2 - 0.5 * (1.2 - 1)
io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid  # xy
io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh  # wh yolo method
io[..., :4] *= self.stride
torch.sigmoid_(io[..., 4:])

我使用这个只得到val上的0.290

io = p.clone()  # inference output
io[..., :2] =  torch.sigmoid(io[..., :2]) * 1.2 - 0.5 * (1.2 - 1)
io[..., :2] = io[..., :2] + self.grid  # xy
io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh  # wh yolo method
io[..., :4] *= self.stride
torch.sigmoid_(io[..., 4:])

我得到了0.475的val精度,我发现我多了一个sigmoid

三个预测层参数不一样是什么意思,我应该如何修改,因为我想对齐AB的性能

GuoQuanhao avatar Sep 24 '21 09:09 GuoQuanhao

三層的scale_x_y分別是1.2, 1.1, 和 1.05.

WongKinYiu avatar Sep 24 '21 09:09 WongKinYiu

谢谢你的提醒,这与cfg文件是一致的,这解决了我的问题

GuoQuanhao avatar Sep 24 '21 09:09 GuoQuanhao

再请问一下conf和iou的设置需要修改吗,我为YOLOLayer增加了scale_x_y参数读取,使其能正确读入各层对应的scale_x_y,但是yolov4-mish在test下为0.404这低于0.415

GuoQuanhao avatar Sep 24 '21 12:09 GuoQuanhao