rt-detr的报错、训练、图片预测和debug

conda create -n rtdetr python=3.8
conda activate rtdetr
conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia
cd /mnt/Vir_new/RT-DETR-main/rtdetr_pytorch
pip install -r requirement.txt


安装环境,一定要按照官网版本的torch:

conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia

不然会报错:

AttributeError: module 'torchvision' has no attribute 'disable_beta_transforms_warning'

 训练指令:

python tools/train.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml

不需要改类别,我以为像detr那样把data_loader的类别改了,就报错咯。不要用改直接训练就行。

数据集路径在:configs/dataset/coco_detection.yml,数据集是coco格式,官网也有格式示例;训练轮数和超参数:configs/rtdetr/include/optimizer.yml;配置文件:configs/rtdetr/rtdetr_r50vd_6x_coco.yml;

导出直接在/mnt/Vir_new/RT-DETR-main/rtdetr_pytorch/tools/export_onnx.py修改模型路径和配置文件路径就行,导出onnx

预测脚本:

import torch
import onnxruntime as ort 
from PIL import Image, ImageDraw
from torchvision.transforms import ToTensor
 
if __name__ == "__main__":
    ##################
    classes = ['','LicensePlate']
    ##################
    # print(onnx.helper.printable_graph(mm.graph))
    #############
    img_path = "/mnt/Vir_new/RT-DETR-main/rtdetr_pytorch/pictures/1.jpg"
    #############
    im = Image.open(img_path).convert('RGB')
    im = im.resize((640, 640))
    im_data = ToTensor()(im)[None]
    print(im_data.shape)
 
    size = torch.tensor([[640, 640]])
    sess = ort.InferenceSession("/mnt/Vir_new/RT-DETR-main/rtdetr_pytorch/onnx_output/model.onnx")
    output = sess.run(
        # output_names=['labels', 'boxes', 'scores'],
        output_names=None,
        input_feed={'images': im_data.data.numpy(), "orig_target_sizes": size.data.numpy()}
    )
 
    # print(type(output))
    # print([out.shape for out in output])
 
    labels, boxes, scores = output
 
    draw = ImageDraw.Draw(im)
    thrh = 0.6
 
    for i in range(im_data.shape[0]):
 
        scr = scores[i]
        lab = labels[i][scr > thrh]
        box = boxes[i][scr > thrh]
 
        print(i, sum(scr > thrh))
        #print(lab)
        print(f'box:{box}')
        for l, b in zip(lab, box):
            draw.rectangle(list(b), outline='red',)
            print(l.item())
            
            draw.text((b[0], b[1] - 10), text=str(classes[l.item()]), fill='blue', )
    #############
    im.save('/mnt/Vir_new/RT-DETR-main/rtdetr_pytorch/pictures/1.jpg')
    #############

DEDUG:直接在train.py上测试,记得pycharm要在2级目录rtdetr-pytorch打开,不然直接和paddle那个平级目录打开会报错找不到 utils这个模块的。

from .utils import get_activation

还有就是打开咯,还要修改导出的内容,像这样,把导出的包路径写详细一点,不然就会报错。具体就是你直接在文件架上搜索路径,加上去就行。

# from .utils import get_activation   这样会报错
from src.zoo.rtdetr.utils import get_activation  改成这样就行

    还有这样的报错也是导出的包路径不对

from .utils import deformable_attention_core_func, get_activation, inverse_sigmoid
ImportError: attempted relative import with no known parent package
 


更新报错:

Image size (2415329280 pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack.

这个错误我在:①/RT-DETR-main/rtdetr_pytorch/src/solver/solver.py;②/RT-DETR-main/rtdetr_pytorch/src/solver/det_solver.py;这2个文件下导包哪里添加下面代码就解决了。
 

from PIL import ImageFile
from PIL import Image
ImageFile.LOAD_TRUNCATED_IMAGES = True
Image.MAX_IMAGE_PIXELS = None

还有一个问题就是,我做的是文字检测,因为检测框是多边形并不是矩形,就会报错我的宽度低于我的x轴,不记得什么报错内容。这个解决的方法就是把检测框全部转化为矩形,然后每个图片的标注内容都要有单独的id,就是1,2,3这样。

另外一个就是训练时候精度是0 ,是因为我之前把我的类别设置为1,其实要加上背景的,RT-DETR-main/rtdetr_pytorch/configs/dataset/coco_detection.yml这里:
 

num_classes: 2
remap_mscoco_category: False

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值