def check_gt_plot2(rgb_file, city_name, city_id, tower_gt): if city_name == 'USA_KS_Colwich_Maize': city_name = 'Colwich_Maize' if city_name == 'NZ_Palmerston North': city_name = 'PalmerstonNorth' try: line_gt = misc_utils.load_file( os.path.join( r'/media/ei-edl01/data/uab_datasets/lines_v3/data/Original_Tiles', '{}{}_GT.png'.format(city_name, city_id))) line_gt = cv2.dilate(line_gt, np.ones((5, 5), np.uint8), iterations=3) plt.figure(figsize=(8, 6)) rgb = misc_utils.load_file(rgb_file) rgb = util_functions.add_mask(rgb, line_gt, [0, 255, 0], 1) plt.imshow(rgb) # visualize_with_connected_pairs(rgb, tower_gt, line_gt, style='r', add_fig=True) center_points = np.array(tower_gt) plt.scatter(center_points[:, 1], center_points[:, 0], c='g', s=40, marker='o', alpha=1, edgecolors='k') plt.title('{} {}'.format(city_name, city_id)) plt.axis('off') plt.tight_layout() plt.show() except: pass
def check_gt_plot(city_name, city_id, tower_gt, line_gt): if city_name == 'Colwich': city_name = 'Colwich_Maize' img_dir = r'/home/lab/Documents/bohao/data/transmission_line/raw' rgb_file = glob( os.path.join(img_dir, '*_{}_{}.tif'.format(city_name, city_id))) line_gt = misc_utils.load_file( os.path.join( r'/media/ei-edl01/data/uab_datasets/lines/data/Original_Tiles', '{}{}_GT.png'.format(city_name, city_id))) line_gt = cv2.dilate(line_gt, np.ones((5, 5), np.uint8), iterations=10) plt.figure(figsize=(8, 6)) assert len(rgb_file) == 1 rgb = misc_utils.load_file(rgb_file[0]) rgb = util_functions.add_mask(rgb, line_gt, [0, 255, 0], 1) plt.imshow(rgb) # visualize_with_connected_pairs(rgb, tower_gt, line_gt, style='r', add_fig=True) center_points = np.array(tower_gt) plt.scatter(center_points[:, 1], center_points[:, 0], c='g', s=40, marker='o', alpha=1, edgecolors='k') plt.title('{} {}'.format(city_name, city_id)) plt.axis('off') plt.tight_layout() plt.show()
def load_data(rgb_dir, conf_dir): img = misc_utils.load_file(os.path.join(rgb_dir, 'USA_{}_{}.tif'.format(city_list[city_id], tile_id))) tower_file = os.path.join(rgb_dir, 'USA_{}_{}.csv'.format(city_list[city_id], tile_id)) tower_gt = read_tower_truth(tower_file) line_file = os.path.join(rgb_dir, 'USA_{}_{}.csv'.format(city_list[city_id], tile_id)) line_gt = read_lines_truth(line_file, tower_gt) line_conf = misc_utils.load_file( os.path.join(conf_dir, '{}{}.png'.format(city_list[city_id].split('_')[1], tile_id))) return img, tower_gt, line_gt, line_conf
def main(): data_dir = r'/media/ei-edl01/data/remote_sensing_data/transmission_line/New_Zealand' city_list = ['Gisborne', 'Palmertson', 'Rotorua', 'Tauranga'] for city in city_list: image_dir = os.path.join(data_dir, 'New_Zealand_{}'.format(city)) img_list = glob(os.path.join(image_dir, '*.tif')) rgb_list = natsorted([a for a in img_list if 'multiclass' not in a and 'resize' not in a]) gt_list = natsorted([a for a in img_list if 'multiclass' in a]) assert len(rgb_list) == len(gt_list) for rgb_file, gt_file in zip(rgb_list, gt_list): print(os.path.basename(rgb_file), os.path.basename(gt_file)) rgb = misc_utils.load_file(rgb_file) gt = misc_utils.load_file(gt_file) visualize.compare_figures([rgb, gt], (1, 2), fig_size=(12, 5))
def load_data(dirs, city_id, tile_id): line_gt = misc_utils.load_file( os.path.join( dirs['line'], '{}{}_GT.png'.format(city_list[city_id].split('_')[1], tile_id))) tower_gt = get_tower_truth_pred(dirs, city_id, tile_id) return line_gt, tower_gt
def write_features(raw_dir, city_list, city_id, tile_id, model): raw_rgb = misc_utils.load_file( os.path.join(raw_dir, 'USA_{}_{}.tif'.format(city_list[city_id], tile_id))) tile_dim = raw_rgb.shape # get tower truth gt_list = [] csv_file_name = os.path.join( raw_dir, 'USA_{}_{}.csv'.format(city_list[city_id], tile_id)) for label, bbox in read_polygon_csv_data(csv_file_name): y, x = get_center_point(*bbox) gt_list.append([y, x]) # get features feature = [] for start, stop, online_points in read_line_csv_data( csv_file_name, gt_list): centers = add_point_if_not_nearby(start, stop, [gt_list[a] for a in online_points]) for c in centers: c = [int(a) for a in c] region = [ c[0] - patch_size[0] // 2, c[1] - patch_size[1] // 2, *patch_size ] region = get_region_bounds(region, tile_dim) img_patch = raw_rgb[region[0]:region[0] + region[2], region[1]:region[1] + region[3], :] feature.append(model.get_feature(img_patch)) return np.array(feature)
def load_data(model_name, ds_name, patch_size): assert model_name in ['unet', 'deeplab'] if model_name == 'unet': model_dir = 'UnetCrop_inria_decay_0_PS(572, 572)_BS5_EP100_LR0.0001_DS60.0_DR0.1_SFN32' else: model_dir = 'DeeplabV3_inria_decay_0_PS(321, 321)_BS5_EP100_LR1e-05_DS40.0_DR0.1_SFN32' result_file = os.path.join(r'/hdd/Results/size', model_dir, '{}_{}'.format(ds_name, patch_size), 'result.txt') results = misc_utils.load_file(result_file) return parse_results(results)
def load_data(rgb_dir, task_dir, city_id, tile_id, model_name='faster_rcnn'): img = misc_utils.load_file( os.path.join(rgb_dir, 'USA_{}_{}.tif'.format(city_list[city_id], tile_id))) pred_vis_tower = misc_utils.load_file( os.path.join( task_dir, 'post_{}_{}_{}_pred2.npy'.format(model_name, city_id, tile_id))) pred_vis_lines = misc_utils.load_file( os.path.join( task_dir, 'post_{}_{}_{}_conn2.npy'.format(model_name, city_id, tile_id))) pred_topo_tower = get_tower_pred( os.path.join(task_dir, model_name, 'USA_{}_{}.txt'.format(city_list[city_id], tile_id))) pred_topo_lines = misc_utils.load_file( os.path.join( task_dir, '{}_graph_rnn_normal_{}_{}.npy'.format(model_name, city_id, tile_id))) pred_combine_tower = pred_vis_tower pred_combine_lines = misc_utils.load_file( os.path.join( task_dir, '{}_graph_rnn_{}_{}.npy'.format(model_name, city_id, tile_id))) tower_file = os.path.join( rgb_dir, 'USA_{}_{}.csv'.format(city_list[city_id], tile_id)) tower_gt = read_tower_truth(tower_file) line_file = os.path.join( rgb_dir, 'USA_{}_{}.csv'.format(city_list[city_id], tile_id)) line_gt = read_lines_truth(line_file, tower_gt) return img, pred_vis_tower, pred_vis_lines, pred_topo_tower, pred_topo_lines, \ pred_combine_tower, pred_combine_lines, tower_gt, line_gt
def get_tower_pred(csv_file): preds = misc_utils.load_file(csv_file) center_list, _, _ = local_maxima_suppression(preds) return center_list
import os import numpy as np from rst_utils import misc_utils, metrics truth_dir = r'/media/ei-edl01/data/uab_datasets/inria/data/Original_Tiles' pred_dir = '/home/lab/Documents/bohao/data/temp_results' model_name = ['CTK', 'CTKA', 'CTK+STN_A'] for mn in model_name: iou_a = [] iou_b = [] for tile_id in range(1, 6): tile_name = 'vienna{}_GT.tif'.format(tile_id) gt = misc_utils.load_file(os.path.join(truth_dir, tile_name)) // 255 pred = misc_utils.load_file(os.path.join(pred_dir, mn, 'inria', 'pred', 'vienna{}.png'.format(tile_id))) iou_a_temp, iou_b_temp = metrics.iou_metric(gt, pred, divide_flag=True) iou_a.append(iou_a_temp) iou_b.append(iou_b_temp) print('{} IoU={:.3f}'.format(mn, np.sum(iou_a)/np.sum(iou_b)))
misc_utils.set_gpu(gpu) model = build_model(input_shape, n_filters, class_num) optm = Adam(lr) lrate = LearningRateScheduler(step_decay) model_ckpt = ModelCheckpoint(os.path.join(model_save_path, 'model.hdf5'), monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto') model.compile(optm, loss='mean_squared_error', metrics=['accuracy', 'binary_crossentropy']) feature_file_name = os.path.join(task_dir, 'mlp_tower_pair_ftr_train.npy') label_file_name = os.path.join(task_dir, 'mlp_tower_pair_lbl_train.npy') feature_file_name_valid = os.path.join(task_dir, 'mlp_tower_pair_ftr_valid.npy') label_file_name_valid = os.path.join(task_dir, 'mlp_tower_pair_lbl_valid.npy') ftr_train = misc_utils.load_file(feature_file_name) ftr_valid = misc_utils.load_file(feature_file_name_valid) lbl_train = misc_utils.load_file(label_file_name) lbl_train = to_categorical(lbl_train, num_classes=class_num) lbl_valid = misc_utils.load_file(label_file_name_valid) lbl_valid = to_categorical(lbl_valid, num_classes=class_num) history = model.fit(ftr_train, lbl_train, epochs=epochs, batch_size=batch_size, validation_data=(ftr_valid, lbl_valid), callbacks=[lrate, model_ckpt]) # make plots plt.figure(figsize=(12, 5)) plt.subplot(121) plt.plot(history.history['binary_crossentropy'], marker='.') plt.plot(history.history['val_binary_crossentropy'], marker='.') '''plt.plot(history.history['mean_squared_error'], marker='.')
for city_id in range(4): graph_train, graph_valid = [], [] centers_train, centers_valid = [], [] for tile_id in range(1, tile_id_list[city_id] + 1): centers = [] print('Evaluating city {} tile {}'.format(city_id, tile_id)) save_file_name = os.path.join( dirs['task'], '{}_{}_cp.npy'.format(city_list[city_id], tile_id)) # load data line_gt, tower_gt = load_data(dirs, city_id, tile_id) # get tower connection info connected_pair = misc_utils.load_file(save_file_name) n_node = len(tower_gt) graph = np.zeros((n_node, n_node)) for cp in connected_pair: graph[cp[0], cp[1]] = 1 graph[cp[1], cp[0]] = 1 for i in range(n_node): centers.append(np.array(tower_gt[i]) / np.array(line_gt.shape)) if tile_id <= 3: graph_valid.append(graph) centers_valid.append(centers) else: graph_train.append(graph)
print('Processing {}: tile {}'.format(city_list[city_id], tile_id)) pb = processBlock.ValueComputeProcess( '{}{}_fe'.format(city_id, tile_id), task_dir, os.path.join(task_dir, '{}_{}_feature.npy'.format(city_id, tile_id)), func=write_features) feature = pb.run(force_run=False, raw_dir=raw_dir, city_list=city_list, city_id=city_id, tile_id=tile_id, model=res50).val raw_rgb = misc_utils.load_file( os.path.join( raw_dir, 'USA_{}_{}.tif'.format(city_list[city_id], tile_id))) tile_dim = raw_rgb.shape # get tower truth gt_list = [] csv_file_name = os.path.join( raw_dir, 'USA_{}_{}.csv'.format(city_list[city_id], tile_id)) for label, bbox in read_polygon_csv_data(csv_file_name): y, x = get_center_point(*bbox) gt_list.append([y, x]) current_cnt = 0 connected_pair = [] all_centers = [] for start, stop, online_points in read_line_csv_data(