-
Notifications
You must be signed in to change notification settings - Fork 3
/
process_wv_noaa_test.py
executable file
·554 lines (435 loc) · 24 KB
/
process_wv_noaa_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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
"""
Copyright 2018 Defense Innovation Unit Experimental
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Modifications copyright (C) 2018 <eScience Institue at University of Washington>
Licensed under CC BY-NC-ND 4.0 License [see LICENSE-CC BY-NC-ND 4.0.markdown for details]
Written by An Yan
"""
'''
This is for creating a multiclass test or validation data for harvey hurricane
In the case of Digital Globe data, there are two classes: damaged buildings / non-damaged buildings
This script produces TF record for validation data or test data
'''
from PIL import Image
import tensorflow as tf
import io
import glob
from tqdm import tqdm
import numpy as np
import logging
import argparse
import os
import json
import wv_util as wv
import tfr_util as tfr
import aug_util as aug
import csv
"""
A script that processes xView imagery.
Args:
image_folder: A folder path to the directory storing xView .tif files
ie ("xView_data/")
json_filepath: A file path to the GEOJSON ground truth file
ie ("xView_gt.geojson")
test_percent (-t): The percentage of input images to use for test set
suffix (-s): The suffix for output TFRecord files. Default suffix 't1' will output
xview_train_t1.record and xview_test_t1.record
augment (-a): A boolean value of whether or not to use augmentation
Outputs:
Writes two files to the current directory containing training and test data in
TFRecord format ('xview_train_SUFFIX.record' and 'xview_test_SUFFIX.record')
"""
def detect_blackblock(img):
# check the # of pixels that with RGB values are all equal to 0
w,h,c = img.shape
black_pixel_count=0
threshold = 0.95 * w * h * 3
non_black_count = np.count_nonzero(img)
if non_black_count > threshold:
return False
else:
return True
def detect_clouds(img, boxes, classes):
mean_threshold_min = 160
w, h, _ = img.shape
#print('w,h', w, h)
var_threshold = 18
rows_to_delete = list()
for i in range(boxes.shape[0]):
xmin, ymin, xmax, ymax = boxes[i]
# ymin = 0
if xmin < 0:
xmin = 0
if ymin<0:
y_min = 0
if xmax > h:
print('xmax > h')
xmax = h
if ymax > w:
print('ymax > w')
ymax= h
#print(xmin, ymin, xmax, ymax)
# clip bbox areas
#cropped_img = img.crop((xmin, ymin, xmax, ymax))
cropped_img = img[int(ymin):int(ymax), int(xmin):int(xmax)] # note the order of w/h
# print(cropped_img)
# print(cropped_img.shape)
array_img = np.array(cropped_img)
mean_img = np.mean(array_img)
#print('mean_img', mean_img, i)
var_img = np.std(array_img)
#print('var_img',var_img, i)
#if var_img < var_threshold and (cropped_img> 150).all() and (cropped_img< 255).all():
if var_img < var_threshold and mean_img > mean_threshold_min:
print('bounding box i has cloud', i)
# need to delete this bbox
rows_to_delete.append(i)
print('rows_to_delete',rows_to_delete)
if len(rows_to_delete) == 0:
return img, boxes, classes
else:
# return boxes and classes with clouds removed
new_coords = np.delete(boxes, rows_to_delete, axis=0)
new_classes = np.delete(classes, rows_to_delete, axis=0)
#new_uids = np.delete(uids, rows_to_delete, axis=0)
return img, new_coords, new_classes
def get_images_from_filename_array(coords,chips,classes,folder_names,res=(200,200)):
"""
Gathers and chips all images within a given folder at a given resolution.
Args:
coords: an array of bounding box coordinates
chips: an array of filenames that each coord/class belongs to.
classes: an array of classes for each bounding box
folder_names: a list of folder names containing images
res: an (X,Y) tuple where (X,Y) are (width,height) of each chip respectively
Output:
images, boxes, classes arrays containing chipped images, bounding boxes, and classes, respectively.
"""
images =[]
boxes = []
clses = []
k = 0
bi = 0
for folder in folder_names:
fnames = glob.glob(folder + "*.tif")
fnames.sort()
for fname in tqdm(fnames):
#Needs to be "X.tif" ie ("5.tif")
name = fname.split("\\")[-1]
arr = wv.get_image(fname)
img,box,cls = wv.chip_image(arr,coords[chips==name],classes[chips==name],res)
for im in img:
images.append(im)
for b in box:
boxes.append(b)
for c in cls:
clses.append(cls)
k = k + 1
return images, boxes, clses
def shuffle_images_and_boxes_classes(im,box,cls):
"""
Shuffles images, boxes, and classes, while keeping relative matching indices
Args:
im: an array of images
box: an array of bounding box coordinates ([xmin,ymin,xmax,ymax])
cls: an array of classes
Output:
Shuffle image, boxes, and classes arrays, respectively
"""
assert len(im) == len(box)
assert len(box) == len(cls)
perm = np.random.permutation(len(im))
out_b = {}
out_c = {}
k = 0
for ind in perm:
out_b[k] = box[ind]
out_c[k] = cls[ind]
k = k + 1
return im[perm], out_b, out_c
'''
Datasets
_multires: multiple resolutions. Currently [(500,500),(400,400),(300,300),(200,200)]
_aug: Augmented dataset
'''
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("image_folder", help="Path to folder containing image chips (ie 'Image_Chips/' ")
parser.add_argument("json_filepath", help="Filepath to GEOJSON coordinate file")
parser.add_argument("-t", "--test_percent", type=float, default=0.333,
help="Percent to split into test (ie .25 = test set is 25% total)")
parser.add_argument("-s", "--suffix", type=str, default='t1',
help="Output TFRecord suffix. Default suffix 't1' will output 'xview_train_t1.record' and 'xview_test_t1.record'")
parser.add_argument("-a","--augment", type=bool, default=False,
help="A boolean value whether or not to use augmentation")
# debug: added percent of data to produce, the purpose is to produce small dataset for fast algorithm development
parser.add_argument("-p", "--sample_percent", type=int, default = 1, help = "Portion to sample data (1/sample_percent) from the original dataset. Meaning that only use a portion of the dataset to construct training and testing. The purpose is for fast algorithm development")
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
#resolutions should be largest -> smallest. We take the number of chips in the largest resolution and make
#sure all future resolutions have less than 1.5times that number of images to prevent chip size imbalance.
#res = [(500,500),(400,400),(300,300),(200,200)]
#res = [(300,300)]
#res = [(512,512)]
res = [(200,200)]
AUGMENT = args.augment
# debug
#SAVE_IMAGES = False
SAVE_IMAGES = True
images = {}
boxes = {}
train_chips = 0
test_chips = 0
num_class1_bbox = 0 # num of bbox of class1
num_class2_bbox = 0
num_class1_chip = 0 # number of chips contain class 1
num_class2_chip = 0
#Parameters
max_chips_per_res = 100000
#train_writer = tf.python_io.TFRecordWriter("harvey_train_%s.record" % args.suffix)
test_writer = tf.python_io.TFRecordWriter("noaa_%s.record" % args.suffix)
#coords,chips,classes = wv.get_labels(args.json_filepath)
coords,chips,classes,uids = wv.get_labels_noaa_w_uids(args.json_filepath)
# debug
#print('number of chips from geojson', len(chips))
#print('number of classes from geojson', len(classes))
#print('some coords: ', coords[2])
#print('some coords: ', coords[3000])
#print('classes some: ', classes[4])
#print('chips ', chips[349])
# debug
sample_percent = args.sample_percent
# a list of classes to be augment. Set to set to be empty if no augmentation
# is wanted
class_to_aug = set([])
num_aug_per_class = {} # class_id: # of augmentation generated
for class_id in class_to_aug:
num_aug_per_class[class_id] = 0
#debug
# for cloud removing and black portion removing
num_cloud_rm = 0 # number of 512 x 512 chips that have clouds removed
num_black = 0 # number of 512 x 512 chips that have black parts
for res_ind, it in enumerate(res):
tot_box = 0
logging.info("Res: %s" % str(it))
ind_chips = 0
fnames = glob.glob(args.image_folder + "*.tif")
fnames.sort()
for fname in tqdm(fnames):
#Needs to be "X.tif", ie ("5.tif")
#Be careful!! Depending on OS you may need to change from '/' to '\\'. Use '/' for UNIX and '\\' for windows
name = fname.split("/")[-1]
# debug
#print('file name: ', name)
arr = wv.get_image(fname)
# debug
print('file name: ', name)
#print('classes[chips==name], ', classes[chips==name])
im,box,classes_final = wv.chip_image(arr,coords[chips==name],classes[chips==name],it)
#Shuffle images & boxes all at once. Comment out the line below if you don't want to shuffle images
im,box,classes_final = shuffle_images_and_boxes_classes(im,box,classes_final)
split_ind = int(im.shape[0] * args.test_percent)
for idx, image in enumerate(im):
if idx%sample_percent !=0:
continue
# debug
print('processing idx: ', idx)
# debug
# remove black block
if detect_blackblock(image):
num_black +=1
continue
# remove clouds
image, new_coords, new_classes = detect_clouds(image,box[idx],classes_final[idx])
if len(new_coords)!= len(box[idx]):
num_cloud_rm += 1
# debug: changed image,box[idx],classes_final[idx] to newly constructed img and box
#tf_example = tfr.to_tf_example(image,box[idx],classes_final[idx])
local_class1 = new_classes[new_classes==1].shape[0]
local_class2 = new_classes[new_classes==2].shape[0]
# debug
# here only write into TF RECORD classes == 1
#tf_example = tfr.to_tf_example(image, new_coords[new_classes ==1], new_classes[new_classes == 1])
tf_example = tfr.to_tf_example(image, new_coords, new_classes)
#Check to make sure that the TF_Example has valid bounding boxes.
#If there are no valid bounding boxes, then don't save the image to the TFRecord.
float_list_value_xmin = tf_example.features.feature['image/object/bbox/xmin'].float_list.value
float_list_value_ymin = tf_example.features.feature['image/object/bbox/ymin'].float_list.value
float_list_value_xmax = tf_example.features.feature['image/object/bbox/xmax'].float_list.value
float_list_value_ymax = tf_example.features.feature['image/object/bbox/ymax'].float_list.value
if (ind_chips < max_chips_per_res and np.array(float_list_value_xmin).any() and np.array(float_list_value_xmax).any() and np.array(float_list_value_ymin).any() and np.array(float_list_value_ymax).any()):
tot_box+=np.array(float_list_value_xmin).shape[0]
num_class1_bbox += local_class1
num_class2_bbox += local_class2
if local_class1 > 0:
num_class1_chip +=1
if local_class2 > 0:
num_class2_chip +=1
#if idx < split_ind:
test_writer.write(tf_example.SerializeToString())
test_chips+=1
if SAVE_IMAGES and idx % 5 ==0:
# debug: changed save dir
#debug
# draw only DAMAGED buildings
#aug.draw_bboxes(image, new_coords[new_classes ==1]).save('./harvey_ms_img_inspect_val_2class_noclean/img_%s_%s.png'%(name,str(idx)))
aug.draw_bboxes(image, new_coords).save('./noaa_val_test_inspect_2class/img_%s_%s.png'%(name,str(idx)))
#else:
# train_writer.write(tf_example.SerializeToString())
# train_chips += 1
# if SAVE_IMAGES:
# debug: changed save dir
#aug.draw_bboxes(image, new_coords[new_classes ==1]).save('./harvey_ms_img_inspect_train_2class_noclean/img_%s_%s.png'%(name,str(idx)))
# aug.draw_bboxes(image, new_coords).save('./harvey_ms_img_inspect_train_2class_noclean/img_%s_%s.png'%(name,str(idx)))
ind_chips +=1
# debug
# store the training and validation images with bboxes for inspection
'''
if SAVE_IMAGES:
# debug: changed save dir
aug.draw_bboxes(image, new_coords).save('./harvey_img_inspect/img_%s_%s.png'%(name,str(idx)))
'''
#Make augmentation probability proportional to chip size. Lower chip size = less chance.
#This makes the chip-size imbalance less severe.
prob = np.random.randint(0,np.max(res))
#for 200x200: p(augment) = 200/500 ; for 300x300: p(augment) = 300/500 ...
# debug
# added customized data augmentation for minor classes
#class_to_aug = [2, 3, 4] # damaged roads, trash heaps, and bridges
# Minor classes will be augmented to 63 times larger with various augmentations
# 1. Detect whether minor classes are in the small chips, if yes, augment
# this chip. The output will be a tensor of augmented images, bboxes, and classes
# unpack the output to tfrecord TRAINING data.
# 2. If the chip does not contain any minor classes, go to normal augmentation
#skip_augmentation = set() # contains a list of chips that contain minor classes
MINOR_CLASS_FLAG = False
for class_id in class_to_aug:
#num_aug_per_class[class_id] = 0
#num_aug_this_class = 0
# debug
# print('checking whether this chip contain class: ', class_id)
# this chip contains minor classes
#if np.any(classes_final[idx][:]== class_id):
#if class_id in set(classes_final[idx]) and idx > split_ind:
if class_id in set(new_classes) and idx > split_ind:
# skip_augmentation.add(idx)
MINOR_CLASS_FLAG = True
# print('trying to call expand_aug for chip: ', idx)
#im_aug,boxes_aug,classes_aug= aug.expand_aug_random(image, box[idx], classes_final[idx], class_id)
# debug
# added to TF RECORD damaged building only
#im_aug,boxes_aug,classes_aug= aug.expand_aug_random(image, new_coords[new_classes ==1], new_classes[new_classes==1], class_id)
im_aug,boxes_aug,classes_aug= aug.expand_aug_random(image, new_coords, new_classes, class_id)
#debug
print('augmentig chip: ', idx)
num_aug = 0
for aug_idx, aug_image in enumerate(im_aug):
# debug
# added to record only damaged buidings
tf_example_aug = tfr.to_tf_example(aug_image, boxes_aug[aug_idx],classes_aug[aug_idx])
#Check to make sure that the TF_Example has valid bounding boxes.
#If there are no valid bounding boxes, then don't save the image to the TFRecord.
float_list_value_xmin = tf_example_aug.features.feature['image/object/bbox/xmin'].float_list.value
float_list_value_xmax = tf_example_aug.features.feature['image/object/bbox/xmax'].float_list.value
float_list_value_ymin = tf_example_aug.features.feature['image/object/bbox/ymin'].float_list.value
float_list_value_ymax = tf_example_aug.features.feature['image/object/bbox/ymax'].float_list.value
# debug
#num_aug = 0
if (np.array(float_list_value_xmin).any() and np.array(float_list_value_xmax).any() and np.array(float_list_value_ymin).any() and np.array(float_list_value_ymax).any()):
tot_box+=np.array(float_list_value_xmin).shape[0]
train_writer.write(tf_example_aug.SerializeToString())
num_aug = num_aug + 1
train_chips+=1
num_aug_per_class[class_id] = num_aug_per_class[class_id]+1
# num_aug_this_class=num_aug_this_class + 1
# debug
if aug_idx%10 == 0 and SAVE_IMAGES:
# debug: changed save dir
aug_image = (aug_image).astype(np.uint8)
aug.draw_bboxes(aug_image,boxes_aug[aug_idx]).save('./MS_expand_aug_random_200/img_aug_%s_%s_%s_%s.png'%(name, str(idx), str(aug_idx), str(class_id)))
# debug
print('augmenting class: ', class_id)
print('number of augmentation: ',num_aug)
#num_aug_per_class[class_id] = num_aug_this_class
# it: iterator for different resolutions
# start to augment the rest
if AUGMENT and prob < it[0] and MINOR_CLASS_FLAG == False:
for extra in range(3):
center = np.array([int(image.shape[0]/2),int(image.shape[1]/2)])
deg = np.random.randint(-10,10)
#deg = np.random.normal()*30
# changed
# remove and gaussian blur
newimg = aug.gaussian_blur(image)
#newimg = image
#.3 probability for each of shifting vs rotating vs shift(rotate(image))
p = np.random.randint(0,3)
# debug
# modified to use the removed cloud version of bboxes
# image, new_coords, new_classes
if p == 0:
newimg,nb = aug.shift_image(newimg,new_coords)
#newimg,nb = aug.shift_image(newimg,box[idx])
elif p == 1:
newimg,nb = aug.rotate_image_and_boxes(newimg,deg,center,new_coords)
#newimg,nb = aug.rotate_image_and_boxes(newimg,deg,center,box[idx])
elif p == 2:
newimg,nb = aug.rotate_image_and_boxes(newimg,deg,center,new_coords)
#newimg,nb = aug.rotate_image_and_boxes(newimg,deg,center,box[idx])
newimg,nb = aug.shift_image(newimg,nb)
newimg = (newimg).astype(np.uint8)
if idx%100 == 0 and SAVE_IMAGES:
#debug
# changed save dir
Image.fromarray(newimg).save('./augmented_img_60/img_%s_%s_%s.png'%(name,extra,it[0]))
if len(nb) > 0:
# debug
# modified to use the cloud removed bboxs
tf_example = tfr.to_tf_example(newimg,nb,new_classes)
#tf_example = tfr.to_tf_example(newimg,nb,classes_final[idx])
#DonI't count augmented chips for chip indices
# changed
# removed data augmentation for test data
if idx < split_ind:
# test_writer.write(tf_example.SerializeToString())
# test_chips += 1
continue
else:
train_writer.write(tf_example.SerializeToString())
train_chips+=1
# debug:
# save image + bounding boxes for debug
#else:
if idx%100 ==0 and SAVE_IMAGES:
# debug: changed save dir
aug.draw_bboxes(newimg,nb).save('./harvey_augmented/img_aug_%s_%s_%s.png'%(name,extra,it[0]))
if res_ind == 0:
max_chips_per_res = int(ind_chips * 1.5)
logging.info("Max chips per resolution: %s " % max_chips_per_res)
logging.info("Tot Box: %d" % tot_box)
logging.info("Chips: %d" % ind_chips)
# debug
for key, val in num_aug_per_class.items():
print('for class:' , key)
print('augmentation applied: ', val)
# debug
print('num of black small chips removed: ', num_black)
print('num of small chips containing clouds:', num_cloud_rm)
print('num of original class 1 bboxes: ', num_class1_bbox)
print('num of original class 2 bboxes: ', num_class2_bbox)
print('num of original chips that contain class 1: ', num_class1_chip)
print('num of original chips that cntain class 2 bboxes: ', num_class2_chip)
#logging.info("saved: %d train chips" % train_chips)
logging.info("saved: %d test chips" % test_chips)
#train_writer.close()
test_writer.close()