allanzelener / YAD2K

YAD2K: Yet Another Darknet 2 Keras
Other
2.71k stars 877 forks source link

Retrain Multiple Bounding Boxes per Image Strange Issue #103

Open maitham opened 6 years ago

maitham commented 6 years ago

Hey, awesome library and thanks all for the great work. @shadySource I have used your retrain script to retrain the model on my own data, however I am running into a rather weird issue where I am not entirely sure whats happening. If I package my data as
{ [image1] : [BBX1], [image1]:[BBX2], [image1] : [BBX3], [image2]:[BBX1], image2:[BBX2] } , rather than {[image1]: [BBX1, BBX2,BBX3], [image2]:[BBX1,BBX2]} i.e. having several copies of an image with each image being fed in several times to the training algorithm with only one bounding box(BBX) per image I seem to be getting better results in terms of the bounding boxes drawn. However this is quite memory intensive and I do run into memory issues doing this. Have I just packaged my data wrong or is it the fact that I am feeding the same image multiple times, thus causing some sort of over-fitting?

Package Script

 def getYOLOBBoxs(xmlFile):
    '''
    INPUTS- XML FILE FOR EXTRACTING DATA
    OUTPUTS - PYTHION DICT CONTAINING APPROPRIATE PROPERTIES OF ANNOTATIONS
    '''
    root = ET.parse(xmlFile).getroot()
    height = float(root[4][1].text)
    width = float(root[4][0].text)
    path = root[2].text

    bboxs = []
    for objectXml in root.findall('object'):
        bbox_class = objectXml.find('name').text
        for bndBox in objectXml.findall('bndbox'):
            xmin = float(bndBox.find('xmin').text)
            ymin = float(bndBox.find('ymin').text)
            xmax = float(bndBox.find('xmax').text)
            ymax = float(bndBox.find('ymax').text)

            YOLO_x_cent = ((xmin + xmax) / 2) / width
            YOLO_y_cent = ((ymin + ymax) / 2) / height
            YOLO_x_width = (xmax - xmin)/width
            YOLO_y_height = (ymax - ymin)/height

            bboxs.append({'path': path, 'bbox_class': bbox_class,'width': width, 'height':height ,'xCent': YOLO_x_cent, 'yCent': YOLO_y_cent, 'boxWidth':YOLO_x_width, 'boxHeight':YOLO_y_height })
    return bboxs

# get latest files
files = [file for file in glob.glob(INPPUTFOLDER + '/*.xml') if getDays(file) < 9]

# Go through files and get appropriate Values 
images=[]
image_labels =[]
bar = progressbar.ProgressBar()

i=0
bboxTests = getYOLOBBoxs(files[0])
labels =[]
for file in bar(files):
    bboxs = getYOLOBBoxs(file)
    for bbox in bboxs:
        resizedWidth = int(bbox['width']/SCALE_FACTOR)
        resizedHeight = int(bbox['height']/SCALE_FACTOR)
        image_labels.append(np.array( [ CLASSES[bbox['bbox_class']], bbox['xCent'] , bbox['yCent'], bbox['boxWidth'], bbox['boxHeight'] ] ))
        img = cv2.resize(cv2.imread(bbox['path']),(resizedWidth,resizedHeight),cv2.INTER_AREA)
        images.append(np.array(img,dtype=np.uint8))
    i+=1

if debug and i == 500:
        break
images = np.asarray(images, dtype=np.uint8)
image_labels = np.array(image_labels)
np.savez("my_dataset", images=images, boxes=image_labels)