解决assert(boxes[:,2]>=boxes[:,0]).all()报错

写这篇主要是写了代码不想浪费

用的是github上的tf-faster-rcnn(自己搜下)

python3.6.2版本

错误:assert(boxes[:,2]>=boxes[:,0]).all():

boxes是标注的bounding box,检查Xmax是否大于Xmin,只要有一个不符合就报错

======================

问题原因:1.数据集有问题2.数据集改过来以后没删除缓存

2问题看https://blog.csdn.net/10km/article/details/64641322,他用的是github上的py-faster-rcnn如果你用的跟我一样,是github上的tf-faster-rcnn的话,清除Annotations里的cache里的pkl..

也评价下https://blog.csdn.net/xzzppp/article/details/52036794里解决方案

如果报错原因是数据集标注起点为(0,0),不想改数据集的话,对Xmin,Ymin,Xmax,Ymax减一去掉可以,但是fipped操作不知道会不会还会有问题,第一条解决方案就还是别用了。。第三个解决方案,如果数据集已经改成是(1,1)为起点的话,还有这个问题就估计是部分标注Xmin和Xmax反过来了

========================

解决1问题:数据集有问题,

可能数据集没符合PASCAL VOC格式,,pascal voc的标注起始应该是1-based而不是以0开始的 

 首先坐标起始点都得为1,坐标不能越界

以下是检查数据集的代码:

1.检查是否图片损坏

2.检查坐标七点是否都不为0

3.检查xmax是否大于xmin,会引起

这个代码作用是检查数据集,检查是否有图片损坏(可能引起loss=NAN),检查是否数据集开始坐标(0,0)

#改一下curDir为数据集里图片的地址和path地址

#1check if there are corrputed images in JPEG
#2check if there are '0' cordinate in xml file
#3chechk if xmin coordinates have 0 value,todo:checkif max is greater than width

#111111111111111111111111
import numpy as np
from PIL import Image 
import  xml.dom.minidom
import os
if __name__ == '__main__': 
    badFilesList = [] 
    curDir = '/media/syy/D/repo/tf-faster-rcnn/data/VOCdevkit2007/VOC2007/JPEGImages'
    for root, dirs, files in os.walk(curDir): # print(files)
        # check the corrupted file in curDir
        for each in files:
            if each.endswith('.jpg') or each.endswith( '.JPG') or each.endswith( '.jpeg') or each.endswith( '.JPEG'): # print(each)
                try: 
                    im = Image.open(os.path.join(root, each)) # im.show()
                    #print(each+"is ok")
                    im= np.array(im,dtype=np.float32)#added recently at Feb.23
                except Exception as e: 
                    print('corrupted file:', os.path.join(root, each)) 
                    badFilesList.append(os.path.join(root, each)) # del the corrupted files
                if len(badFilesList) != 0:
                    for each in badFilesList: 
                        try: 
                            #os.remove(each)
                            print("pretend to remove"+each) 
                        except Exception as e: 
                            print('Del file: %s failed, %s' % (each, e)) 
                            pass				
    if len(badFilesList) == 0:
        print("no corrupted filesï¼already checked ",len(files))
    else:
        print("corrputed files are above----------------------------","already checked ",len(files))
#222=222222222222222222222222
    #pause_=input()
    path=r"/media/syy/D/repo/tf-faster-rcnn/data/VOCdevkit2007/VOC2007/Annotations/"
    Assertion_provoker_y = []
    Assertion_provoker_x = []
    files = os.listdir(path)
    for file in files:
        file=os.path.join(path,file)
        dom = xml.dom.minidom.parse(file)
        root = dom.documentElement
        size=dom.getElementsByTagName("size")

        height=dom.getElementsByTagName("height")[0]
        width=dom.getElementsByTagName("width")[0]
        width=width.childNodes[0].data
        height=height.childNodes[0].data

        objects=dom.getElementsByTagName("object")
        for object in objects:
            bndbox = object.getElementsByTagName('bndbox')[0]########
            xmin = bndbox.getElementsByTagName('xmin')[0]
            xmin_data=xmin.childNodes[0].data
            ymin = bndbox.getElementsByTagName('ymin')[0]
            ymin_data=ymin.childNodes[0].data
            xmax = bndbox.getElementsByTagName('xmax')[0]
            xmax_data=xmax.childNodes[0].data
            ymax = bndbox.getElementsByTagName('ymax')[0]
            ymax_data=ymax.childNodes[0].data
#check if the xmin or yin is 0
            if(int(xmin_data) ==0):
                print(file,"the x of it is 0")               
            if(int(ymin_data) ==0):
                print(file,"the y of it is 0")
##check if min is greater than max
            if( int(ymin_data) > int(ymax_data) ):
                Assertion_provoker_y.append(file)
            if( int(xmin_data) > int(xmax_data) ):
                Assertion_provoker_x.append(file)
#check if x is greater than width,or y is greater than height
            if(int(xmax_data)>int(width)):
                Assertion_provoker_x.append(file)
            if(int(xmin_data)>int(width)):
                Assertion_provoker_x.append(file)
            if(int(ymax_data)>int(height)):
                Assertion_provoker_y.append(file)
            if(int(ymin_data)>int(height)):
                Assertion_provoker_y.append(file)
    print("checking assertion error done,already checked",len(files),"files,with {} y_provoker".format(len(Assertion_provoker_y)))
    print("checking assertion error done,already checked",len(files),"files,with {} x_provoker".format(len(Assertion_provoker_x)))
    #print(Assertion_provoker_x)
#######################################################

===========================下面是我写的处理一些越界问题的代码,比如xmin与xmax相反,最后没用上,别浪费自己的一番心血。。。贴上来(可能下面的代码会有小错误)==================================

from xml.etree.ElementTree import ElementTree,Element
import  xml.dom.minidom
import os
path="/media/syy/D/repo/tf-faster-rcnn/data/VOCdevkit2007/VOC2007/Annotations/"
Assertion_provoker_x = []
Assertion_provoker_y = []
sucess_file = []
files = os.listdir(path)
for file in files:
    file=os.path.join(path,file)
    dom = xml.dom.minidom.parse(file)
    root = dom.documentElement
    objects=dom.getElementsByTagName("object")
    for object in objects:
        flag = False
        bndbox = object.getElementsByTagName('bndbox')[0]
        xmin = bndbox.getElementsByTagName('xmin')[0]
        xmin_data=xmin.childNodes[0].data
        xmax = bndbox.getElementsByTagName('xmax')[0]
        xmax_data=xmin.childNodes[0].data

        ymin = bndbox.getElementsByTagName('ymin')[0]
        ymin_data=xmax.childNodes[0].data
        ymax = bndbox.getElementsByTagName('ymax')[0]
        ymax_data=xmax.childNodes[0].data

#----------#below is switch xmin and xmax------------------
        if( int(xmin_data) > int(xmax_data) ):
            Assertion_provoker_x.append(file)            
            xmin.childNodes[0].data = xmax_data
            xmax.childNodes[0].data = xmin_data
            flag = True
        if( int(ymin_data) > int(ymax_data) ):
            Assertion_provoker_y.append(file)
            ymin.childNodes[0].data = ymax_data
            ymax.childNodes[0].data = ymin_data
            flag = True  
           #above is switch the ymin with ymax

        if flag:
          #  with open(os.path.join(path,file),'w',encoding='UTF-8') as fh:
           #     dom.writexml(fh)
            sucess_file.append(file)    
#-------------
print("checking assertion error done,already checked",len(files),"files,with {} X_provoker".format(len(Assertion_provoker_x)),'\nand {} y_provokers '.format(len(Assertion_provoker_y)))
print("sucessfully dealt with {} files ".format(len(sucess_file)))


参考:

【1】https://blog.csdn.net/10km/article/details/64641322

【2】https://blog.csdn.net/xzzppp/article/details/52036794

=================================================================

参考:https://blog.csdn.net/hongxingabc/article/details/79048531

某些常见报错

https://blog.csdn.net/xzzppp/article/details/52036794

猜你喜欢

转载自blog.csdn.net/ptgood/article/details/87351183
今日推荐