Commit 9aba5b3f authored by Gaetano Raffaele's avatar Gaetano Raffaele
Browse files

ENH: allow multiple nomenclature levels in a single classification run.

No related merge requests found
Showing with 27 additions and 21 deletions
+27 -21
...@@ -97,21 +97,22 @@ class ObjectBasedClassifier: ...@@ -97,21 +97,22 @@ class ObjectBasedClassifier:
} }
return models, summary, results return models, summary, results
def classify(self, model, perc=None, output_file=None, compress='NONE'): def classify(self, models, perc=None, output_files=None, compress='NONE'):
prg = tqdm(desc='Classification', total=len(self.obia_base.tiles)) if isinstance(output_files, str):
if isinstance(model, list): models, output_files = [models], [output_files]
for t, L, X in self.obia_base.tiled_data(normalize=perc): prg = tqdm(desc='Classification', total=len(self.obia_base.tiles) * len(models))
prob = [] for t, L, X in self.obia_base.tiled_data(normalize=perc):
for m in model: for model, output_file in zip(models, output_files):
prob.append(m.predict_proba(X)) if isinstance(model, list):
prob = np.prod(prob, axis=0) prob = []
c = model[0].classes_[np.argmax(prob, axis=1)] for m in model:
self.obia_base.populate_map(t, L, c, output_file, compress) prob.append(m.predict_proba(X))
prg.update(1) prob = np.prod(prob, axis=0)
else: c = model[0].classes_[np.argmax(prob, axis=1)]
for t,L,X in self.obia_base.tiled_data(normalize=perc): self.obia_base.populate_map(t, L, c, output_file, compress)
c = model.predict(X) else:
self.obia_base.populate_map(t, L, c, output_file, compress) c = model.predict(X)
self.obia_base.populate_map(t, L, c, output_file, compress)
prg.update(1) prg.update(1)
return return
......
...@@ -54,7 +54,7 @@ class OBIABase: ...@@ -54,7 +54,7 @@ class OBIABase:
self.n_vars = 0 self.n_vars = 0
# Output raster # Output raster
self.output_map = None self.output_maps = []
def init_ref_db(self, vector_file, id_field, class_field): def init_ref_db(self, vector_file, id_field, class_field):
if isinstance(class_field, str): if isinstance(class_field, str):
...@@ -321,6 +321,10 @@ class OBIABase: ...@@ -321,6 +321,10 @@ class OBIABase:
X = (X - normalize[0]) / (normalize[1] - normalize[0]) X = (X - normalize[0]) / (normalize[1] - normalize[0])
yield tilenum,L,X yield tilenum,L,X
def create_new_map(self):
self.output_maps.append(np.zeros((self.H, self.W), dtype=np.int))
return
def populate_map(self, tilenum, obj_id, classes, output_file=None, compress='NONE'): def populate_map(self, tilenum, obj_id, classes, output_file=None, compress='NONE'):
r = otb.itkRegion() r = otb.itkRegion()
r['index'][0], r['index'][1], r['size'][0], r['size'][1] = self.tiles[tilenum] r['index'][0], r['index'][1], r['size'][0], r['size'][1] = self.tiles[tilenum]
...@@ -331,9 +335,7 @@ class OBIABase: ...@@ -331,9 +335,7 @@ class OBIABase:
tmp = np.zeros(np.max(tile_obj) + 1, dtype=int) tmp = np.zeros(np.max(tile_obj) + 1, dtype=int)
tmp[obj_id] = classes tmp[obj_id] = classes
if output_file is None: if output_file is None:
if self.output_map is None: self.output_map[-1][self.tiles[tilenum][1]:self.tiles[tilenum][1]+self.tiles[tilenum][3],
self.output_map = np.zeros((self.H,self.W), dtype=np.int)
self.output_map[self.tiles[tilenum][1]:self.tiles[tilenum][1]+self.tiles[tilenum][3],
self.tiles[tilenum][0]:self.tiles[tilenum][0]+self.tiles[tilenum][2]] += tmp[tile_obj] self.tiles[tilenum][0]:self.tiles[tilenum][0]+self.tiles[tilenum][2]] += tmp[tile_obj]
else: else:
if not os.path.exists(os.path.dirname(output_file)): if not os.path.exists(os.path.dirname(output_file)):
......
...@@ -80,10 +80,13 @@ def classify(seg, ts_lst_pkl, m_files, d, map_files): ...@@ -80,10 +80,13 @@ def classify(seg, ts_lst_pkl, m_files, d, map_files):
obc = ObjectBasedClassifier(seg, obc = ObjectBasedClassifier(seg,
ts_lst, ts_lst,
unroll_file_list(d['userfeat'])) unroll_file_list(d['userfeat']))
for m_file, map_file in zip(m_files, map_files): models = []
for m_file in m_files:
with open(m_file, 'rb') as mf: with open(m_file, 'rb') as mf:
m_dict = pickle.load(mf) m_dict = pickle.load(mf)
obc.classify(m_dict['model'], perc=[m_dict['perc2'], m_dict['perc98']], output_file=map_file) models.append(m_dict['model'])
perc = [m_dict['perc2'], m_dict['perc98']]
obc.classify(models, perc=perc, output_files=map_files)
return return
def report(map_files, m_files, d, report_files): def report(map_files, m_files, d, report_files):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment