diff --git a/Learning/ObjectBased.py b/Learning/ObjectBased.py index 46c459e8a4dd697f0e7ad827eeff77f79b957382..1fa7787723341ec8e7e831372e958d22c568cad4 100644 --- a/Learning/ObjectBased.py +++ b/Learning/ObjectBased.py @@ -8,8 +8,8 @@ from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import confusion_matrix, accuracy_score, cohen_kappa_score, precision_recall_fscore_support class ObjectBasedClassifier: - def __init__(self, object_layer, reference_data, time_series_patterns, user_feature_list): - self.obia_base = OBIABase(object_layer, ref_data = reference_data) + def __init__(self, object_layer, reference_data, time_series_patterns, user_feature_list, ref_class_field='class'): + self.obia_base = OBIABase(object_layer, ref_data = reference_data, ref_class_field=ref_class_field) for ptrn in time_series_patterns: lst = sorted(glob.glob(ptrn)) self.obia_base.add_raster_time_series_for_stats(lst) @@ -113,20 +113,19 @@ class ObjectBasedClassifier: #TEST CODE def run_test(): - ''' obc = ObjectBasedClassifier('/DATA/Moringa_Sample/Parakou/output/segmentation/segmentation.tif', '/DATA/Moringa_Sample/Parakou/input/REF/ref.shp', ['/DATA/Moringa_Sample/Parakou/output/S2_processed/T31PDL/*/*FEAT.tif'], ['/DATA/Moringa_Sample/Parakou/input/THR/THR_SPOT6.tif']) ''' - print('Initializing the OBIA training base...') obc = ObjectBasedClassifier('/DATA/Benin/OBSYDYA_data/MORINGA/SEGMENTATION/segmentation.tif', '/DATA/Benin/OBSYDYA_data/MORINGA/reference/BD_OBSYDYA_2022_ParakouNdali_v0.2.shp', ['/DATA/Benin/OBSYDYA_data/MORINGA/basefolder/FEAT/S2_THEIA_FEAT/S2_THEIA_MOSAIC_*.tif'], glob.glob('/DATA/Benin/OBSYDYA_data/MORINGA/ext_features')) + ''' obc.gen_k_folds(5) print('Performing Training and Cross-Validation...') - m,s,r = obc.train_RF(400) + m,s,r = obc.train_RF(100) print(s) print('Performing Classification...') obc.classify(m) diff --git a/OBIA/OBIABase.py b/OBIA/OBIABase.py index ff0bd8d8f78e1a9a116e2411c1190b0a1f9ee6be..3e264e49b4b97bd6b7c715037f5a1c9e434445ef 100644 --- a/OBIA/OBIABase.py +++ b/OBIA/OBIABase.py @@ -9,6 +9,7 @@ from enum import Enum from Common.otb_numpy_proc import to_otb_pipeline from skimage.measure import regionprops, label import otbApplication as otb +from tqdm import tqdm class OStats(Enum): MEAN = 'mean' @@ -88,7 +89,7 @@ class OBIABase: columns=['area', 'orig_label', 'polygon_id', 'class'], index=[]) r = otb.itkRegion() - for tn, t in self.tiles.items(): + for tn, t in tqdm(self.tiles.items(), desc='Init. Ref. DB', total=len(self.tiles)): r['index'][0], r['index'][1] = t[0], t[1] r['size'][0], r['size'][1] = t[2], t[3] ref_ol.PropagateRequestedRegion('out', r) @@ -99,9 +100,9 @@ class OBIABase: self.ref_db = pd.concat([ self.ref_db, pd.DataFrame( - data=[np.insert(o.intensity_min, 0, o.area) for o in rp], + data=[np.insert(o.intensity_min, 0, o.area) for o in rp if self.obj_to_tile[o.label] == tn], columns=self.ref_db.columns, - index=[o.label for o in rp] + index=[o.label for o in rp if self.obj_to_tile[o.label] == tn] )] ) @@ -112,19 +113,22 @@ class OBIABase: W, H = in_seg.GetImageSize('out') r = otb.itkRegion() obj_bbox = {} - for i in range(0, W, nominal_tile_size[0]): - for j in range(0, H, nominal_tile_size[1]): - r['index'][0], r['index'][1] = i, j - r['size'][0], r['size'][1] = min(nominal_tile_size[0], W-i), min(nominal_tile_size[1], H-j) - in_seg.PropagateRequestedRegion('out', r) - tile = in_seg.GetImageAsNumpyArray('out') - rp = regionprops(tile.astype(np.uint32)) - for o in rp: - bbox = np.array([o.bbox[0]+j, o.bbox[1]+i, o.bbox[2]+j, o.bbox[3]+i]) - if o.label in obj_bbox.keys(): - obj_bbox[o.label].append(bbox) - else: - obj_bbox[o.label] = [bbox] + rw, rh = range(0, W, nominal_tile_size[0]), range(0, H, nominal_tile_size[1]) + with tqdm(total=len(rw)*len(rh), desc='Gen. tile info') as pb: + for i in rw: + for j in rh: + r['index'][0], r['index'][1] = i, j + r['size'][0], r['size'][1] = min(nominal_tile_size[0], W-i), min(nominal_tile_size[1], H-j) + in_seg.PropagateRequestedRegion('out', r) + tile = in_seg.GetImageAsNumpyArray('out') + rp = regionprops(tile.astype(np.uint32)) + for o in rp: + bbox = np.array([o.bbox[0]+j, o.bbox[1]+i, o.bbox[2]+j, o.bbox[3]+i]) + if o.label in obj_bbox.keys(): + obj_bbox[o.label].append(bbox) + else: + obj_bbox[o.label] = [bbox] + pb.update(1) for o in obj_bbox.keys(): obj_bbox[o] = np.array(obj_bbox[o]) @@ -252,7 +256,7 @@ class OBIABase: def populate_ref_db(self): assert(self.ref_db is not None) - for t in self.tiles.keys(): + for t in tqdm(self.tiles.keys(), desc="Comp. zonal stats on DB", total=len(self.tiles)): for rf, rs, rv in zip(self.raster_files, self.raster_stats, self.raster_var_names): k, v = self.compute_stats_on_tile(t, rf, rs, on_ref=True) if k is not None: