-
Notifications
You must be signed in to change notification settings - Fork 35
/
test.py
executable file
·391 lines (298 loc) · 14.6 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import os
import sys
import gdal
import cv2
import numpy as np
import time as mtime
import argparse
import logging
# from matplotlib import pyplot as plt
from keras.models import load_model
from keras.models import Model
from keras import backend as K
import tensorflow as tf
from src import postprocess
from src import metric
from src import io
from src import util
from src import bf_grid
from src import metric
from src import dataGenerator
from src import model
import config
# physical_devices = tf.config.experimental.list_physical_devices('GPU')
# assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
# _config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
util.check_dir(config.path_logs)
util.set_logger(os.path.os.path.join(config.path_logs, 'testing.log'))
parser = argparse.ArgumentParser(
description='See description below to see all available options')
parser.add_argument('-sg', '--skipGridding',
help='If skipping grididing while testing. [Default] False',
type=bool,
default=False,
required=False)
parser.add_argument('-d', '--data',
help='Input Data folder where TIF files are stored',
type=str,
required=True)
parser.add_argument('-ups', '--upscalling',
help='UpScale TIF image to predefined 10cm image',
type=bool,
required=True)
parser.add_argument('-pt', '--pretrained',
help='Path of pretrained complete model or weight file.\
Use -w flag to mark it as weight or complete model',
type=str,
required=True)
parser.add_argument('-w', '--weight',
help='If model provided is Model Weight or not. \
True - It is Weight, False- Complete Model',
type=bool,
required=True)
parser.add_argument('-lf', '--linearFeature',
help='If the object is linear feature like road? \
[Default] False',
type=bool,
default=False,
required=False)
parser.add_argument('-o', '--output',
help='Output Data folder where TIF files will be saved',
type=str,
required=True)
args = parser.parse_args()
path_data = args.data
st_time = mtime.time()
logging.info('Input data given: {}'.format(path_data))
logging.info('percent_overlap : {}'.format(config.overlap))
# Storing time of process here
timing = {}
# Current running process
logging.info('Initilization')
# Filer for post processing
filter = config.erosion_filter
simplify_parameter = config.simplify_parameter # in metres
# Results path
path_result = args.output
path_tiled = os.path.join(path_result, 'tiled')
path_predict = os.path.join(path_result, 'prediction')
path_merged_prediction = os.path.join(path_result, 'merged_prediction')
path_erosion = os.path.join(path_merged_prediction, 'erosion')
path_watershed = os.path.join(path_merged_prediction, 'watershed')
path_vector = os.path.join(path_merged_prediction, 'vector')
path_simplify = os.path.join(path_merged_prediction, 'simplify')
path_bbox = os.path.join(path_merged_prediction, 'bbox')
# Creating directory
util.check_dir(path_result)
util.check_dir(path_predict)
util.check_dir(path_tiled)
util.check_dir(path_merged_prediction)
util.check_dir(path_erosion)
util.check_dir(path_watershed)
util.check_dir(path_vector)
util.check_dir(path_simplify)
util.check_dir(path_bbox)
# Logging output paths
logging.info('Result path is %s' % (path_result))
logging.info('Predict path is %s' % (path_predict))
logging.info('Tile image path is %s' % (path_merged_prediction))
logging.info('Erosion path is %s' % (path_erosion))
logging.info('watershed path is %s' % (path_watershed))
logging.info('vector path is %s' % (path_vector))
logging.info('simplify path is %s' % (path_simplify))
logging.info('bbox path is %s' % (path_bbox))
# loading model from model file or weights file
logging.info('Loading trained model')
if args.weight is True:
unet_model = model.unet(config.image_size)
try:
unet_model.load_weights(args.pretrained)
except Exception as e:
msg = 'Unable to load model weights: {}'.format(args.pretrained)
logging.error(msg)
raise('{}. Error : {}'.format(msg, e))
else:
try:
unet_model = load_model(args.pretrained, custom_objects={
'dice_coef': metric.dice_coef, 'jaccard_coef': metric.jaccard_coef})
except Exception as e:
msg = 'Unable to load model: {}'.format(args.pretrained)
logging.error(msg)
raise('{}. Error : {}'.format(msg, e))
# Iterating over all the files
for root, dirs, files in os.walk(path_data):
for file in files:
if file.endswith(tuple(config.image_ext)):
temp_path_data = os.path.join(root, file)
if args.upscalling is True:
logging.info('Scaling TIF to custom resolution')
# Converting to 10 cm data
res = config.default_resolution
gdalOption = gdal.WarpOptions(
format='VRT', xRes=res, yRes=res)
# Creating output file name
path_image_output = os.path.join(root, util.getNamenoExt(file) + '_' + str(res)+'.vrt')
# Creating VRT of input image
gdal.Warp(path_image_output, os.path.abspath(os.path.join(
root, file)), options=gdalOption)
# Replacing default image with 10 cm upscaled image
temp_path_data = path_image_output
# Creating a new folder for tiled and prediction of the file
_name = os.path.splitext(os.path.basename(file))[0]
temp_path_tiled = os.path.join(path_tiled, os.path.basename(_name))
temp_path_predict = os.path.join(
path_predict, os.path.basename(_name))
# Creating a folder in the name of file
util.check_dir(temp_path_tiled)
util.check_dir(temp_path_predict)
logging.info('Gridding image : {}'.format(temp_path_data))
if args.skipGridding is False:
time = mtime.time()
bf_grid.grid_file(path_data=temp_path_data,
path_output=temp_path_tiled)
logging.info('Gridding Completed: {}'.format(temp_path_data))
# Loading the Testing image
logging.info('Reading Gridded image: {}'.format(temp_path_data))
testing_dataList = dataGenerator.getTestingData(
path_tile_image=temp_path_tiled)
testing_list_ids, testing_imageMap = testing_dataList.getList()
logging.info('Total number of files gridded for {} : {}'.format(
temp_path_data, len(testing_list_ids)))
# get name, size, geo referencing data map
logging.info('Extracting GeoData from Gridded data')
testing_geoMap = io.getGeodata(testing_imageMap)
# Testing DataGenerator
logging.info('Testing Generator')
training_generator = dataGenerator.DataGenerator(
list_IDs=testing_list_ids,
imageMap=testing_imageMap,
labelMap=None,
batch_size=config.batch,
n_classes=None,
image_channels=config.num_image_channels,
label_channels=None,
image_size=config.image_size,
prediction=True,
shuffle=False)
# Prediction model
logging.info('Predicting data: {}'.format(temp_path_data))
predictResult = unet_model.predict_generator(
generator=training_generator,
workers=6,
use_multiprocessing=True,
verbose=1
)
logging.info('Number of data files predicted for {} : {} is shape of predicted matrix'.format(
temp_path_data, predictResult.shape))
logging.info('Saving Prediction: {}'.format(temp_path_data))
predict_image = []
# Iterating over predictions and saving it to geoReferenced TIF files
temp_listPrediction = []
for i in range(len(testing_list_ids)):
file_name = os.path.basename(testing_geoMap[i]['path'])
labelPrediction = predictResult[i, :, :, :]
# Setting 0.5 as threshold
labelPrediction = np.round(labelPrediction, decimals=0)
temp_path_output = os.path.join(temp_path_predict, file_name)
# Saving data to disk
io.write_tif(temp_path_output, labelPrediction*255, testing_geoMap[i]['geoTransform'],
testing_geoMap[i]['geoProjection'], testing_geoMap[i]['size'])
temp_listPrediction.append(temp_path_output)
timing['Processing'] = mtime.time() - st_time
# Merging Gridded dataset to single TIF
time = mtime.time()
logging.info('Merging and compressing gridded dataset: {}. \
Total number of files: {}. This may take a while'.format(temp_path_data, len(temp_listPrediction)))
temp_merged_output = os.path.join(path_merged_prediction, file)
io.mergeTile(listTIF=temp_listPrediction,
path_output=temp_merged_output)
# # merging completed
# timing[current_process[-1]] = mtime.time() - time
temp_erosion_output = os.path.join(path_erosion, file)
# Post Processing output image
if args.linearFeature is False:
# Post processing erosion
logging.info('Post Processing erosion')
time = mtime.time()
postprocess.erosion(path_input=temp_merged_output,
filter=filter,
path_output=temp_erosion_output
)
# Erosion completed
logging.info(
'Erosion has been completed: {}'.format(temp_path_data))
# Watershed segmentation
neighbour = config.watershed_neighbour
logging.info('Post Processing watershed_segmentation')
time = mtime.time()
temp_watershed_output = os.path.join(path_watershed, file)
# Processing Watershed Segmentation
postprocess.watershedSegmentation(
temp_erosion_output, neighbour, temp_watershed_output)
# Watershed segmentation completed
logging.info(
'Watershed Segmentation Completed: {}'.format(temp_path_data))
timing['WatershedSegmentation'] = mtime.time() - time
# Converting raster to Vector
time = mtime.time()
logging.info('Converting Raster to vector')
temp_raster2vector_output = os.path.join(path_vector, _name)
print('Input to raster2vector: {}'.format(temp_watershed_output))
temp_raster2vector_output = io.raster2vector(path_raster=temp_watershed_output,
path_output=temp_raster2vector_output)
logging.info(
'raster2vector Completed: {}'.format(temp_path_data))
# # Vectorization completed
# timing[current_process[-1]] = mtime.time() - time
# Since we are making a vector file for each raster band, thus there can be multiple... \
# ... vector files for one raster
# Simplification of polygons
logging.info('Simplifying Vectors')
for _temp_vector_path in temp_raster2vector_output:
temp_simplify_output = os.path.join(
path_simplify, _name, os.path.basename(_temp_vector_path))
util.check_dir(os.path.dirname(temp_simplify_output))
postprocess.simplify_polygon(path_shp=_temp_vector_path,
parameter=config.simplify_parameter,
path_output=temp_simplify_output)
logging.info(
'Vector simplification Completed: {}'.format(temp_path_data))
# Shp to axis aligned bounding box
logging.info(
'Post Processing vectors to bounding box')
# current_process.append('aabbox')
time = mtime.time()
temp_bbox_output = os.path.join(
path_bbox, _name, os.path.basename(_temp_vector_path))
util.check_dir(os.path.dirname(temp_bbox_output))
postprocess.aabbox(path_shp=temp_simplify_output,
path_output=temp_bbox_output)
logging.info(
'AA BBox Completed: {}'.format(temp_path_data))
# aabbox completed
# timing[current_process[-1]] = mtime.time() - time
elif args.linearFeature is True:
logging.info('Post Processing skeletonization')
time = mtime.time()
path_skeleton = os.path.join(
path_merged_prediction, 'path_skeleton')
util.check_dir(path_skeleton)
temp_skeletonize_output = os.path.join(path_skeleton, file)
postprocess.skeletonize(
path_input=temp_erosion_output, path_output=temp_skeletonize_output)
# Skeletonization completed
# timing[current_process[-1]] = mtime.time() - time
# Converting raster to Vector
time = mtime.time()
logging.info('Converting Raster to vector')
temp_raster2vector_output = os.path.join(path_vector, _name)
io.raster2vector(path_raster=temp_skeletonize_output,
path_output=temp_raster2vector_output)
logging.info(
'raster2vector Completed: {}'.format(temp_path_data))
# Vectorization completed
# timing[current_process[-1]] = mtime.time() - time
# Saving to JSON
io.tojson(timing, os.path.join(path_result, 'Timing.json'))
logging.info('Process Completed')
sys.exit()