name = fname.split("/")[-1] #arr = wv.get_image(fname) with rasterio.open(fname, "r") as r: arr = r.read() arr = np.rollaxis(arr, 0, 3) 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): tf_example = tfr.to_tf_example(image, box[idx], classes_final[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 = tf_example.features.feature[ 'image/object/bbox/xmin'].float_list.value if (ind_chips < max_chips_per_res and np.array(float_list_value).any()): tot_box += np.array(float_list_value).shape[0] if idx < split_ind: test_writer.write(tf_example.SerializeToString()) test_chips += 1 else: train_writer.write(tf_example.SerializeToString())
# debug # get statistics about number of damaged buildings and non-damaged buildings #print("type of new_classes: ", type(new_classes)) # print('new_classes', new_classes) #print('new_classes[new_classes==1]: ', new_classes.count(1)) # number of class 1 bbox in the small chip 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()
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] if len(new_coords) == 1 and np.all(new_coords == 0): print('This chip contains no bboxes, removing...') continue # 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]