Пример #1
0
    def __init__(self, report_name=None, method=None, 
                        folder_name=None, save_imgs=True, out_path=None, 
                        detections=None, in_path=None, 
                        detector_names=None, 
                        mergeFalsePos=False,
                        separateDetections=True,
                        vocGood=0.1):
        '''
        Will score the detections class it contains.

        Parameters:
        --------------------
        separateDetections: bool
            Whether the metal detections can count as true positives for the thatch 
            detections and viceversa
        mergeFalsePos: bool
            Whether we need to keep track of the good and bad detections of metal
            and thatch separately or not. We cannot separate them if we want to 
            train a single neural network to distinguish between both types of roofs 
            since the bad detections of either roof must not contain a positive detection
            of the other type of roof (it should be background)
        '''
        self.save_imgs = save_imgs
        #these two are related to saving the FP and TP for neural training
        self.mergeFalsePos=mergeFalsePos

        self.keep_detections_separate = separateDetections  

        #threholds to classify detections as False/True positives and Good/Bad detections(these are to train the neural network on it)
        self.VOC_threshold = utils.VOC_threshold #threshold to assign a detection as a true positive
        self.VOC_good_detection_threshold = dict()
        self.VOC_good_detection_threshold['metal'] = utils.VOC_threshold 
        self.VOC_good_detection_threshold['thatch'] = utils.VOC_threshold
        self.detection_portion_threshold = 0.50

        self.detections = detections    #detection class

        self.in_path = in_path          #the path from which the images are taken
        self.img_names = [f for f in listdir(self.in_path) if f.endswith('.jpg')]

        #the ground truth roofs for every image and roof type
        self.correct_roofs = dict()
        for roof_type in utils.ROOF_TYPES:
            self.correct_roofs[roof_type] = dict()
        for img_name in self.img_names:
            for roof_type in utils.ROOF_TYPES:
                self.correct_roofs[roof_type][img_name] = DataLoader.get_polygons(roof_type=roof_type, 
                                                            xml_name=img_name[:-3]+'xml' , xml_path=self.in_path)
                self.detections.update_roof_num(self.correct_roofs[roof_type][img_name], roof_type)

        #init the report file
        self.out_path = out_path
        if detector_names is not None:
            self.init_report(detector_names, report_name=report_name)

        #variables needed to pickle the FP and TP to a file that makes sense
        assert method is not None
        self.method = method
        self.folder_name = folder_name
Пример #2
0
    def setup_augmented_patches():
        '''
        No division between different roof sizes: if a roof has a size that is off, we resize it
        Make them lie down, save patches to folder
        Augment patches, save them to augmented folder
        '''
        in_path = utils.get_path(in_or_out=utils.IN, data_fold=utils.TRAINING)
        out_path = utils.get_path(viola=True, in_or_out=utils.IN, data_fold=utils.TRAINING)

        img_names_list = [img_name for img_name in os.listdir(in_path) if img_name.endswith('.jpg')]

        for roof_type in ['metal', 'thatch']:
            for img_id, img_name in enumerate(img_names_list):

                print 'Processing image: {0}'.format(img_name)
                img_path = in_path+img_name

                polygon_list = DataLoader.get_polygons(roof_type=roof_type, xml_name=img_name[:-3]+'xml', xml_path=in_path, padding=)
                roof_patches = DataLoader.extract_patches(polygon_list, img_path=img_path, grayscale=True)

                for roof_id, roof_img in enumerate(roof_patches):
                    print 'Processing image {0}: roof {1}'.format(img_id, roof_id)
                       
                    #if it's vertical, make it lie down
                    if roof_img.shape[0] > roof_img.shape[1]:
                        roof_img = DataAugmentation.rotateImage(roof_img, clockwise=True)
                    
                    #write basic positive example to the right folder
                    general_path = '{0}{1}_{2}_{3}'.format(out_path, roof_type, img_name[:-4], roof_id)

                    #calculate and write the augmented images 
                    for i in range(4):
                        roof_img_cp = np.copy(roof_img)

                        if i == 1:
                            roof_img_cp = cv2.flip(roof_img_cp,flipCode=0)
                        elif i == 2:
                            roof_img_cp = cv2.flip(roof_img_cp,flipCode=1)
                        elif i==3:
                            roof_img_cp = cv2.flip(roof_img_cp,flipCode=-1)

                        write_to_path = '{0}_flip{1}.jpg'.format(general_path, i)
                        cv2.imwrite(write_to_path, roof_img_cp)
Пример #3
0
    def __init__(self,
                 report_name=None,
                 method=None,
                 full_dataset=True,
                 folder_name=None,
                 save_imgs=True,
                 out_path=None,
                 detections=None,
                 in_path=None,
                 detector_names=None,
                 mergeFalsePos=False,
                 separateDetections=True,
                 vocGood=0.1,
                 negThres=0.3,
                 auc_threshold=0.5,
                 correct_roofs=None,
                 img_names=None):
        '''
        Will score the detections class it contains.

        Parameters:
        --------------------
        separateDetections: bool
            Whether the metal detections can count as true positives for the thatch 
            detections and viceversa
        mergeFalsePos: bool
            Whether we need to keep track of the good and bad detections of metal
            and thatch separately or not. We cannot separate them if we want to 
            train a single neural network to distinguish between both types of roofs 
            since the bad detections of either roof must not contain a positive detection
            of the other type of roof (it should be background)
        '''
        self.TOTAL = 0
        self.save_imgs = save_imgs
        #these two are related to saving the FP and TP for neural training
        self.mergeFalsePos = mergeFalsePos
        self.auc_threshold = auc_threshold

        self.keep_detections_separate = separateDetections

        #threholds to classify detections as False/True positives and Good/Bad detections(these are to train the neural network on it)
        self.VOC_threshold = utils.VOC_threshold  #threshold to assign a detection as a true positive
        self.VOC_good_detection_threshold = dict()
        self.VOC_good_detection_threshold['metal'] = utils.VOC_threshold
        self.VOC_good_detection_threshold['thatch'] = utils.VOC_threshold
        self.detection_portion_threshold = 0.50
        self.negThres = negThres

        self.detections = detections
        self.in_path = in_path  #the path from which the images are taken
        if img_names is None:
            self.img_names = [
                f for f in listdir(self.in_path) if f.endswith('.jpg')
            ]
        else:
            self.img_names = img_names

        #the ground truth roofs for every image and roof type
        if correct_roofs is None:
            self.correct_roofs = dict()
            for roof_type in utils.ROOF_TYPES:
                self.correct_roofs[roof_type] = dict()
            self.full_dataset = full_dataset
            if full_dataset == False:
                for img_name in self.img_names:
                    for roof_type in utils.ROOF_TYPES:
                        #we receive polygons, so we convert them into boxes so we can use the fast scoring
                        temp_roofs = DataLoader.get_polygons(
                            roof_type=roof_type,
                            xml_name=img_name[:-3] + 'xml',
                            xml_path=self.in_path)
                        if len(temp_roofs) > 0:
                            self.correct_roofs[roof_type][
                                img_name] = utils.polygons2boxes(temp_roofs)
                        else:
                            self.correct_roofs[roof_type][img_name] = []
                        self.detections.update_roof_num(
                            self.correct_roofs[roof_type][img_name], roof_type)
            else:
                for img_name in self.img_names:
                    current_roofs = DataLoader.get_all_roofs_full_dataset(
                        xml_name=img_name[:-3] + 'xml', xml_path=self.in_path)
                    for roof_type in utils.ROOF_TYPES:
                        if roof_type not in current_roofs:
                            self.correct_roofs[roof_type][img_name] = []
                        else:
                            self.correct_roofs[roof_type][
                                img_name] = current_roofs[roof_type]

                        self.detections.update_roof_num(
                            self.correct_roofs[roof_type][img_name], roof_type)
        else:
            self.correct_roofs = correct_roofs

        #init the report file
        self.out_path = out_path
        if report_name is not None:
            if detector_names is not None:
                self.init_report(detector_names, report_name=report_name)

        #variables needed to pickle the FP and TP to a file that makes sense
        assert method is not None
        self.method = method
        self.folder_name = folder_name