def generateValidData(batch_size, data=[]): # print 'generateValidData...' while True: valid_data = [] valid_label = [] batch = 0 for i in (range(len(data))): url = data[i] batch += 1 # img = load_img(train_data_path + 'src/' + url) img = load_img_by_gdal(train_data_path + 'src/' + url, img_w, img_h) # Adapt dim_ordering automatically img = img_to_array(img) valid_data.append(img) label = load_img(train_data_path + 'label/' + url, grayscale=True) label = img_to_array(label) valid_label.append(label) if batch % batch_size == 0: valid_data = np.array(valid_data) valid_label = np.array(valid_label) valid_label = to_categorical(valid_label, num_classes=n_label) valid_label = valid_label.reshape( (batch_size, img_w * img_h, n_label)) yield (valid_data, valid_label) valid_data = [] valid_label = [] batch = 0
def generateData(batch_size, data=[]): # print 'generateData...' while True: train_data = [] train_label = [] batch = 0 for i in (range(len(data))): url = data[i] batch += 1 # img = load_img(train_data_path + 'src/' + url) img = load_img_by_gdal(train_data_path + 'src/' + url, img_w, img_h) # Adapt dim_ordering automatically img = img_to_array(img) train_data.append(img) label = load_img(train_data_path + 'label/' + url, grayscale=True) label = img_to_array(label) train_label.append(label) if batch % batch_size == 0: # print 'get enough bacth!\n' train_data = np.array(train_data) train_label = np.array(train_label) train_label = to_categorical( train_label, num_classes=n_label) # one_hot coding train_label = train_label.reshape( (batch_size, img_w * img_h, n_label)) yield (train_data, train_label) train_data = [] train_label = [] batch = 0
def get_hist(files, bands=4, scale=1024): print("[Info] Statisify histogram from images...") hist = np.zeros((scale, bands), np.uint64) in_files = [] if isinstance(files, str): in_files.append(files) elif isinstance(files, list): in_files = files # in_files=list(in_files) for file in in_files: print("\n\t[info]deal:{}".format(file)) img = load_img_by_gdal(file) a, b, c = img.shape if c != bands: print( "Warning: bands of img is :{}, but setting bands is:{}".format( c, bands)) real_b = min(c, bands) for i in range(real_b): h, bin_edges = np.histogram(img[:, :, i], bins=range(scale + 1)) h = np.array(h, np.uint64) hist[:, i] += h[:scale] # plt.plot(range(scale), h) # plt.show() return hist
def predict_multiclass(img_file, out_path): print("[INFO] opening image...") input_img = load_img_by_gdal(img_file) if im_type == UINT8: input_img = input_img / 255.0 elif im_type == UINT10: input_img = input_img / 1024.0 elif im_type == UINT16: input_img = input_img / 65535.0 input_img = np.clip(input_img, 0.0, 1.0) """checke model file""" print("model file: {}".format(model_file)) if not os.path.isfile(model_file): print("model does not exist:{}".format(model_file)) sys.exit(-2) model = load_model(model_file) abs_filename = os.path.split(img_file)[1] abs_filename = abs_filename.split(".")[0] print(abs_filename) if FLAG_APPROACH_PREDICT == 0: print("[INFO] predict image by orignal approach\n") result = orignal_predict_onehot(input_img, im_bands, model, window_size) output_file = ''.join( [out_path, '/original_predict_', abs_filename, '_multiclass.png']) print("result save as to: {}".format(output_file)) cv2.imwrite(output_file, result * 128) elif FLAG_APPROACH_PREDICT == 1: print("[INFO] predict image by smooth approach\n") result = predict_img_with_smooth_windowing_multiclassbands( input_img, model, window_size=window_size, subdivisions=2, real_classes=target_class, # output channels = 是真的类别,总类别-背景 pred_func=smooth_predict_for_multiclass, PLOT_PROGRESS=False) for b in range(target_class): output_file = ''.join([ out_path, '/mask_multiclass_', abs_filename, '_', dict_target[b], '.png' ]) cv2.imwrite(output_file, result[:, :, b]) print("Saved to: {}".format(output_file)) gc.collect()
def predict_binary_jaccard(img_file, output_file): print("[INFO] opening image...") input_img = load_img_by_gdal(img_file) if im_type == UINT8: input_img = input_img / 255.0 elif im_type == UINT10: input_img = input_img / 1024.0 elif im_type == UINT16: input_img = input_img / 65535.0 input_img = np.clip(input_img, 0.0, 1.0) input_img = input_img.astype(np.float16) model = load_model(model_file) if FLAG_APPROACH_PREDICT==0: print("[INFO] predict image by orignal approach\n") result = orignal_predict_notonehot(input_img,im_bands, model, window_size) abs_filename = os.path.split(img_file)[1] abs_filename = abs_filename.split(".")[0] # output_file = ''.join([output_path, '/original_pred_', # abs_filename, '_', dict_target[FLAG_TARGET_CLASS],'_jaccard.png']) output_file = ''.join([output_path, '/mask_binary_', abs_filename, '_', dict_target[FLAG_TARGET_CLASS], '_jaccard_original.png']) print("result save as to: {}".format(output_file)) cv2.imwrite(output_file, result*128) elif FLAG_APPROACH_PREDICT==1: print("[INFO] predict image by smooth approach\n") result = predict_img_with_smooth_windowing_multiclassbands( input_img, model, window_size=window_size, subdivisions=2, real_classes=target_class, # output channels = 是真的类别,总类别-背景 pred_func=smooth_predict_for_binary_notonehot, PLOT_PROGRESS=False ) cv2.imwrite(output_file, result) print("Saved to: {}".format(output_file)) gc.collect()
def select_invalid_values(self, filepath): files, num = get_file(filepath) assert (num != 0) for label_file in tqdm(files): # label_file = input_label_path + os.path.split(src_file)[1] # # ret,src_img = load_img(src_file) # assert(ret==0) label_img = load_img_by_gdal(label_file, grayscale=True) label_img = np.array(label_img) local_labels = np.unique(label_img) invalid_labels = [] self.HAS_INVALID_VALUE = False for tmp in local_labels: if tmp not in self.valid_values: invalid_labels.append(tmp) print("\nWarning: some label is not valid value") print("\nFile: {}".format(label_file)) self.HAS_INVALID_VALUE = True if self.HAS_INVALID_VALUE == True: new_label_img = self.make_invalid_to_zeros( label_img, invalid_labels) new_label_file = os.path.split( label_file)[0] + '/new_' + os.path.split(label_file)[1] cv2.imwrite(new_label_file, new_label_img) self.HAS_INVALID_VALUE = False label_img = new_label_img plt.imshow(label_img, cmap='gray') plt.show() print("Check completely!\n")
def produce_training_samples_binary(in_path, out_path, image_num=50000, mode='original'): print('\ncreating dataset...') label_files, tt = get_file(os.path.join(in_path, 'label/')) assert (tt != 0) image_each = image_num / len(label_files) print("\n[INFO] produce samples---------------------") g_count = 0 for label_file in tqdm(label_files): src_file = os.path.join(in_path, 'src/') + os.path.split(label_file)[1] if not os.path.isfile(src_file): print("Have no file:".format(src_file)) continue print("src file:{}".format(os.path.split(src_file)[1])) # label_img = cv2.imread(label_file, cv2.IMREAD_GRAYSCALE) label_img = load_img_by_gdal(label_file, grayscale=True) # print("label_img: {}".format(np.unique(label_img))) label_img = label_img.astype(np.uint8) y, x = label_img.shape # print("label_img: {}".format(np.unique(label_img))) dataset = gdal.Open(src_file) if dataset == None: print("open failed!\n") continue Y_height = dataset.RasterYSize X_width = dataset.RasterXSize # check size of label and src images x, y = label_img.shape if (X_width != x and Y_height != y): print("label and source image have different size:".format( label_file)) continue im_bands = dataset.RasterCount data_type = dataset.GetRasterBand(1).DataType src_img = dataset.ReadAsArray(0, 0, X_width, Y_height) src_img = np.array(src_img) del dataset index = np.where(label_img == target_label) all_label = np.zeros((Y_height, X_width), np.uint8) all_label[index] = 1 print(np.unique(all_label)) count = 0 while count < image_each: random_width = random.randint(0, X_width - img_w - 1) random_height = random.randint(0, Y_height - img_h - 1) src_roi = src_img[:, random_height:random_height + img_h, random_width:random_width + img_w] label_roi = all_label[random_height:random_height + img_h, random_width:random_width + img_w] """ignore nodata area""" FLAG_HAS_NODATA = False tmp = np.unique(label_img[random_height:random_height + img_h, random_width:random_width + img_w]) for tt in tmp: if tt not in valid_labels: FLAG_HAS_NODATA = True continue if FLAG_HAS_NODATA == True: continue """ignore pure background area""" if len(np.unique(label_roi)) < 2: if 0 in np.unique(label_roi): continue if mode == 'augment': src_roi, label_roi = data_augment(src_roi, label_roi, data_type) visualize = label_roi * 50 cv2.imwrite((out_path + '/visualize/%d.png' % g_count), visualize) cv2.imwrite((out_path + '/label/%d.png' % g_count), label_roi) src_sample_file = out_path + '/src/%d.png' % g_count driver = gdal.GetDriverByName("GTiff") # driver = gdal.GetDriverByName("PNG") # outdataset = driver.Create(src_sample_file, img_w, img_h, im_bands, gdal.GDT_UInt16) outdataset = driver.Create(src_sample_file, img_w, img_h, im_bands, data_type) if outdataset == None: print("create dataset failed!\n") sys.exit(-2) if im_bands == 1: outdataset.GetRasterBand(1).WriteArray(src_roi) else: for i in range(im_bands): outdataset.GetRasterBand(i + 1).WriteArray(src_roi[i]) del outdataset count += 1 g_count += 1
# model=binary_segnet(img_w, img_h, im_bands, n_label) input_layer = Input((img_w, img_h, im_bands)) model = Unet_resnet_model(input_layer, img_w, 16, 0.5) # print("Train by : {}".format(dict_network[FLAG_USING_NETWORK])) print("Train by : Unet+resnet") train(model) if FLAG_MAKE_TEST: print("test ....................predict by trained model .....\n") test_img_path = '../../data/test/sample1.png' import sys if not os.path.isfile(test_img_path): print("no file: {}".format(test_img_path)) sys.exit(-1) input_img = load_img_by_gdal(test_img_path) if im_type == UINT8: input_img = input_img / 255.0 elif im_type == UINT10: input_img = input_img / 1024.0 elif im_type == UINT16: input_img = input_img / 65535.0 input_img = np.clip(input_img, 0.0, 1.0) new_model = load_model(model_save_path) test_predict(input_img, new_model)
def predict_binary_for_single_image(input_dict={}): gup_id = input_dict['GPUID'] os.environ["CUDA_VISIBLE_DEVICES"] = gup_id window_size = input_dict['windsize'] im_bands = input_dict['im_bands'] im_type = input_dict['dtype'] FLAG_APPROACH_PREDICT = input_dict['strategy'] img_file = input_dict['image_file'] model_file = input_dict['model_file'] output_file = input_dict['mask_path'] out_bands = 1 FLAG_ONEHOT = 0 if input_dict['onehot']: FLAG_ONEHOT = 1 input_img = load_img_by_gdal(img_file) if im_type == UINT8: input_img = input_img / 255.0 elif im_type == UINT10: input_img = input_img / 1024.0 elif im_type == UINT16: input_img = input_img / 65535.0 input_img = np.clip(input_img, 0.0, 1.0) input_img = input_img.astype(np.float16) # test accuracy """checke model file""" print("model file: {}".format(model_file)) if not os.path.isfile(model_file): print("model does not exist:{}".format(model_file)) sys.exit(-2) model = load_model(model_file) if FLAG_APPROACH_PREDICT == 0: print("[INFO] predict image by orignal approach\n") if FLAG_ONEHOT: result = orignal_predict_onehot(input_img, im_bands, model, window_size) else: result = orignal_predict_notonehot(input_img, im_bands, model, window_size) print("result save as to: {}".format(output_file)) cv2.imwrite(output_file, result * 128) elif FLAG_APPROACH_PREDICT == 1: print("[INFO] predict image by smooth approach\n") if FLAG_ONEHOT: result = predict_img_with_smooth_windowing_multiclassbands( input_img, model, window_size=window_size, subdivisions=2, real_classes=out_bands, # output channels = 是真的类别,总类别-背景 pred_func=smooth_predict_for_binary_onehot) else: result = predict_img_with_smooth_windowing_multiclassbands( input_img, model, window_size=window_size, subdivisions=2, real_classes=out_bands, # output channels = 是真的类别,总类别-背景 pred_func=smooth_predict_for_binary_notonehot) print("result save as to: {}".format(output_file)) cv2.imwrite(output_file, result) print("Saved to {}".format(output_file)) gc.collect() return 0
def predict_multiclass_for_batch_image(input_dict={}): gup_id = input_dict['GPUID'] os.environ["CUDA_VISIBLE_DEVICES"] = gup_id window_size = input_dict['windsize'] im_bands = input_dict['im_bands'] im_type = input_dict['dtype'] FLAG_APPROACH_PREDICT = input_dict['strategy'] input_path = input_dict['image_dir'] model_file = input_dict['model_file'] output_path = input_dict['mask_dir'] out_bands = input_dict['target_num'] all_files, num = get_file(input_path) if num == 0: print("There is no file in path:{}".format(input_path)) sys.exit(-1) for img_file in all_files: print("[INFO] opening image...".format(img_file)) input_img = load_img_by_gdal(img_file) if im_type == UINT8: input_img = input_img / 255.0 elif im_type == UINT10: input_img = input_img / 1024.0 elif im_type == UINT16: input_img = input_img / 65535.0 input_img = np.clip(input_img, 0.0, 1.0) input_img = input_img.astype(np.float16) model = load_model(model_file) print(model.summary()) layer_dict = dict([(layer.name, layer) for layer in model.layers]) layer_name = 'softmax' #sigmoid, softmax nb_classes = layer_dict[layer_name].output.shape[-1] if out_bands != nb_classes - 1: out_bands = nb_classes - 1 abs_filename = os.path.split(img_file)[1] abs_filename = abs_filename.split(".")[0] if FLAG_APPROACH_PREDICT == 0: print("[INFO] predict image by orignal approach\n") result = orignal_predict_onehot(input_img, im_bands, model, window_size) output_file = ''.join([output_path, '/', abs_filename, '.png']) print("result save as to: {}".format(output_file)) cv2.imwrite(output_file, result * 128) elif FLAG_APPROACH_PREDICT == 1: print("[INFO] predict image by smooth approach\n") result = predict_img_with_smooth_windowing_multiclassbands( input_img, model, window_size=window_size, subdivisions=2, real_classes=out_bands, # output channels = 是真的类别,总类别-背景 pred_func=smooth_predict_for_multiclass, PLOT_PROGRESS=False) H, W, C = np.array(input_img).shape output_file = ''.join( [output_path, '/', abs_filename, '_smooth_pred.png']) output_mask = np.zeros((H, W), np.uint8) for i in range(out_bands): indx = np.where(result[:, :, i] >= 127) output_mask[indx] = i + 1 print(np.unique(result)) cv2.imwrite(output_file, output_mask) print("Saved to:{}".format(output_file)) # for b in range(out_bands): # output_file = ''.join([output_path, '/', abs_filename, '_', dict_target[b],'smooth.png']) # cv2.imwrite(output_file, result[:,:,b]) # print("Saved to: {}".format(output_file)) gc.collect() return 0
def predict_binary_for_batch_image(input_dict={}): gup_id = input_dict['GPUID'] os.environ["CUDA_VISIBLE_DEVICES"] = gup_id window_size = input_dict['windsize'] im_bands = input_dict['im_bands'] im_type = input_dict['dtype'] FLAG_APPROACH_PREDICT = input_dict['strategy'] input_path = input_dict['image_dir'] model_file = input_dict['model_file'] output_path = input_dict['mask_dir'] out_bands = 1 FLAG_ONEHOT = 0 if input_dict['onehot']: FLAG_ONEHOT = 1 all_files, num = get_file(input_path) if num == 0: print("There is no file in path:{}".format(input_path)) sys.exit(-1) for img_file in all_files: print("[INFO] opening image...") print("FileName:{}".format(img_file)) input_img = load_img_by_gdal(img_file) if im_type == UINT8: input_img = input_img / 255.0 elif im_type == UINT10: input_img = input_img / 1024.0 elif im_type == UINT16: input_img = input_img / 65535.0 input_img = np.clip(input_img, 0.0, 1.0) input_img = input_img.astype(np.float16) model = load_model(model_file) print(model.summary()) layer_dict = dict([(layer.name, layer) for layer in model.layers]) layer_name = 'sigmoid' # sigmoid, softmax nb_classes = layer_dict[layer_name].output.shape[-1] abs_filename = os.path.split(img_file)[1] abs_filename = abs_filename.split(".")[0] if FLAG_APPROACH_PREDICT == 0: print("[INFO] predict image by orignal approach\n") if FLAG_ONEHOT: result = orignal_predict_onehot(input_img, im_bands, model, window_size) else: result = orignal_predict_notonehot(input_img, im_bands, model, window_size) output_file = ''.join([output_path, '/', abs_filename, '.png']) print("result save as to: {}".format(output_file)) cv2.imwrite(output_file, result * 128) elif FLAG_APPROACH_PREDICT == 1: print("[INFO] predict image by smooth approach\n") if FLAG_ONEHOT: result = predict_img_with_smooth_windowing_multiclassbands( input_img, model, window_size=window_size, subdivisions=2, real_classes=out_bands, # output channels = 是真的类别,总类别-背景 pred_func=smooth_predict_for_binary_onehot, PLOT_PROGRESS=False) else: result = predict_img_with_smooth_windowing_multiclassbands( input_img, model, window_size=window_size, subdivisions=2, real_classes=out_bands, # output channels = 是真的类别,总类别-背景 pred_func=smooth_predict_for_binary_notonehot, PLOT_PROGRESS=False) output_file = ''.join( [output_path, '/', abs_filename, 'smooth.png']) cv2.imwrite(output_file, result) print("Saved to: {}".format(output_file)) gc.collect() return 0
label_list, img_list =[], [] label_files, _=get_file(inputdir+'/label') img_files =[] for file in label_files: absname = os.path.split(file)[1] absname = absname.split('.')[0] img_f = find_file(inputdir+'/src',absname) img_files.append(img_f) # img_files = list() # img_files, _=get_file(inputdir+'/src') assert(len(label_files)==len(img_files)) name_list =[] for i,file in enumerate(label_files): l_img = load_img_by_gdal(file, grayscale=True) if len(l_img)==0: continue label_list.append(l_img) absname = os.path.split(file)[1] only_name = absname.split('.')[0] name_list.append(only_name) # src_file = inputdir+'/src/'+absname s_img = load_img_by_gdal(img_files[i]) if len(s_img)==0: continue img_list.append(s_img) assert(len(label_list)==len(img_list)) try:
def produce_training_samples_binary(self): print('\ncreating dataset...') in_path = self.input_dict['input_dir'] out_path = self.input_dict['output_dir'] valid_labels = list(range(int(self.input_dict['min']), int(self.input_dict['max']+1))) target_label = int(self.input_dict['target_label']) label_files, tt = get_file(os.path.join(in_path, 'label/')) assert (tt != 0) image_num = int(self.input_dict['sample_num']) image_each = image_num / len(label_files) img_w = int(self.input_dict['window_size']) img_h = int(self.input_dict['window_size']) print("\n[INFO] produce samples---------------------") g_count = 0 for label_file in tqdm(label_files): src_file = os.path.join(in_path, 'src/') + os.path.split(label_file)[1] if not os.path.isfile(src_file): print("Have no file:".format(src_file)) continue print("src file:{}".format(os.path.split(src_file)[1])) label_img = load_img_by_gdal(label_file, grayscale=True) # print("label_img: {}".format(np.unique(label_img))) label_img = label_img.astype(np.uint8) y, x = label_img.shape # print("label_img: {}".format(np.unique(label_img))) dataset = gdal.Open(src_file) if dataset == None: print("open failed!\n") continue Y_height = dataset.RasterYSize X_width = dataset.RasterXSize if (X_width != x and Y_height != y): print("label and source image have different size:".format(label_file)) continue im_bands = dataset.RasterCount data_type = dataset.GetRasterBand(1).DataType src_img = dataset.ReadAsArray(0, 0, X_width, Y_height) src_img = np.array(src_img) del dataset index = np.where(label_img == target_label) all_label = np.zeros((Y_height, X_width), np.uint8) all_label[index] = 1 print(np.unique(all_label)) # if no pixel in target value, ignore this label file tp = np.unique(all_label) # if tp[0]==0: # print("no target value in {}".format(label_file)) # continue # if len(tp) < 2: print("Only one value {} in {}".format(tp, label_file)) if tp[0] == 0: print("no target value in {}".format(label_file)) continue count = 0 while count < image_each: random_width = random.randint(0, X_width - img_w - 1) random_height = random.randint(0, Y_height - img_h - 1) src_roi = src_img[:, random_height: random_height + img_h, random_width: random_width + img_w] label_roi = all_label[random_height: random_height + img_h, random_width: random_width + img_w] """ignore nodata area""" FLAG_HAS_NODATA = False tmp = np.unique(label_img[random_height: random_height + img_h, random_width: random_width + img_w]) for tt in tmp: if tt not in valid_labels: FLAG_HAS_NODATA = True continue if FLAG_HAS_NODATA == True: continue """ignore pure background area""" if len(np.unique(label_roi)) < 2: if 0 in np.unique(label_roi): continue if 'augment' in self.input_dict['mode']: src_roi, label_roi = self.data_augment(src_roi, label_roi, img_w, img_h, data_type) visualize = label_roi * 50 cv2.imwrite((out_path + '/visualize/%d.png' % g_count), visualize) cv2.imwrite((out_path + '/label/%d.png' % g_count), label_roi) src_sample_file = out_path + '/src/%d.png' % g_count driver = gdal.GetDriverByName("GTiff") # driver = gdal.GetDriverByName("PNG") # outdataset = driver.Create(src_sample_file, img_w, img_h, im_bands, gdal.GDT_UInt16) outdataset = driver.Create(src_sample_file, img_w, img_h, im_bands, data_type) if outdataset == None: print("create dataset failed!\n") sys.exit(-2) if im_bands == 1: outdataset.GetRasterBand(1).WriteArray(src_roi) else: for i in range(im_bands): outdataset.GetRasterBand(i + 1).WriteArray(src_roi[i]) del outdataset count += 1 g_count += 1
def accuracy_evalute(input_dict): ref_file = input_dict['gt_file'] pred_file = input_dict['mask_file'] valid_labels = input_dict['valid_values'] n_class = len(valid_labels) check_rate = input_dict['check_rate'] # gup_id = input_dict['GPUID'] # os.environ["CUDA_VISIBLE_DEVICES"] = gup_id ref_img = load_img_by_gdal(ref_file, grayscale=True) pred_img = load_img_by_gdal(pred_file, grayscale=True) print("\nfile: {}".format(os.path.split(pred_file)[1])) print("[INFO] Calculate confusion matrix..\n") ref_img = np.array(ref_img) height, width = ref_img.shape print(height, width) if height != pred_img.shape[0] or width != pred_img.shape[1]: print("image sizes of reference and predicted are not equal!\n") img_length = height * width if check_rate < 0.01: check_rate = 0.01 elif check_rate > 1.00: check_rate = 1.00 else: pass # assert (check_rate > 0.001 and check_rate <= 1.00) num_checkpoints = np.int(img_length * check_rate) pos = random.sample(range(img_length), num_checkpoints) """reshape images from two to one dimension""" ref_img = np.reshape(ref_img, height * width) pred_img = np.reshape(pred_img, height * width) labels = ref_img[pos] """ignore nodata pixels""" valid_index = [] for tt in valid_labels: ind = np.where(labels == tt) ind = list(ind) valid_index.extend(ind[0]) valid_index.sort() valid_num_checkpoints = len(valid_index) print( "{}points have been selected, and {} valid points will be used to evaluate accuracy!\n" .format(num_checkpoints, valid_num_checkpoints)) # valid_ref=ref_img[valid_index] valid_ref = labels[valid_index] print("valid value in reference image: {}".format(np.unique(valid_ref))) ts = pred_img[pos] valid_pred = ts[valid_index] print("valid value in predicton image: {}".format(np.unique(valid_pred))) tmp = np.unique(valid_pred) """make nodata to zero in predicted results""" for ss in tmp: if not ss in valid_labels: nodata_index = np.where(valid_pred == ss) valid_pred[nodata_index] = 0 confus_matrix = confusion_matrix(valid_ref, valid_pred, range(n_class)) # confus_matrix = tf.contrib.metrics.confusion_matrix(valid_pred, valid_ref, n_class) # with tf.Session() as sess: # confus_matrix = sess.run(confus_matrix) print(confus_matrix) confus_matrix = np.array(confus_matrix) oa = 0 x_row_plus = [] x_col_plus = [] x_diagonal = [] for i in range(n_class): oa += confus_matrix[i, i] x_diagonal.append(confus_matrix[i, i]) row_sum = sum(confus_matrix[i, :]) col_sum = sum(confus_matrix[:, i]) x_row_plus.append(row_sum) x_col_plus.append(col_sum) print("x_row_plus:{}".format(x_row_plus)) print("x_col_plus:{}".format(x_col_plus)) x_row_plus = np.array(x_row_plus) x_col_plus = np.array(x_col_plus) x_diagonal = np.array(x_diagonal) x_total = sum(x_row_plus) OA_acc = oa / (sum(x_row_plus)) print("\nOA:{:.3f}".format(OA_acc)) tmp = x_col_plus * x_row_plus kappa = (x_total * sum(x_diagonal) - sum(x_col_plus * x_row_plus) ) / np.float(x_total * x_total - sum(x_col_plus * x_row_plus)) print("Kappa:{:.3f}".format(kappa)) for i in range(n_class): i = i prec = x_diagonal[i] / (x_row_plus[i] + SMOOTH) print("\nForground of {}".format(i)) print("\nprecision= {:.3f}".format(prec)) recall = x_diagonal[i] / (x_col_plus[i] + SMOOTH) print("recall= {:.3f}".format(recall)) F1_score = (2 * recall * prec) / (recall + prec) print("F1_score= {:.3f}".format(F1_score)) iou = x_diagonal[i] / (x_row_plus[i] + x_col_plus[i] - x_diagonal[i] + SMOOTH) print("iou= {:.3f}".format(iou))
def accuracy_evalute(input_dict): ref_file = input_dict['gt_file'] pred_file = input_dict['mask_file'] valid_labels = input_dict['valid_values'] n_class = len(valid_labels) check_rate = input_dict['check_rate'] gup_id = input_dict['GPUID'] os.environ["CUDA_VISIBLE_DEVICES"] = gup_id # ret, ref_img = load_img_by_cv2(ref_file, grayscale=True) # if ret != 0: # print("Open file failed: {}".format(ref_file)) # sys.exit(-1) # ret, pred_img = load_img_by_cv2(pred_file, grayscale=True) # print(np.unique(pred_img)) # if ret != 0: # print("Open file failed: {}".format(pred_file)) # sys.exit(-2) ref_img = load_img_by_gdal(ref_file, grayscale=True) pred_img = load_img_by_gdal(pred_file, grayscale=True) print("\nfile: {}".format(os.path.split(pred_file)[1])) print("[INFO] Calculate confusion matrix..\n") ref_img = np.array(ref_img) height, width = ref_img.shape print(height, width) if height != pred_img.shape[0] or width != pred_img.shape[1]: print("image sizes of reference and predicted are not equal!\n") img_length = height * width assert (check_rate > 0.001 and check_rate <= 1.00) num_checkpoints = np.int(img_length * check_rate) pos = random.sample(range(img_length), num_checkpoints) """reshape images from two to one dimension""" ref_img = np.reshape(ref_img, height * width) pred_img = np.reshape(pred_img, height * width) labels = ref_img[pos] """ignore nodata pixels""" valid_index = [] for tt in valid_labels: ind = np.where(labels == tt) ind = list(ind) valid_index.extend(ind[0]) valid_index.sort() valid_num_checkpoints = len(valid_index) print( "{}points have been selected, but {} points will be used to evaluate accuracy!\n" .format(num_checkpoints, valid_num_checkpoints)) # valid_ref=ref_img[valid_index] valid_ref = labels[valid_index] print("valid value in reference image: {}".format(np.unique(valid_ref))) ts = pred_img[pos] valid_pred = ts[valid_index] print("valid value in predicton image: {}".format(np.unique(valid_pred))) tmp = np.unique(valid_pred) for ss in tmp: assert (ss in valid_labels) """Test to find out where are labels in the confusion matrix""" # num_tmp = labels[labels==0].shape # print("label 0 : {}".format(num_tmp)) # num_tmp = labels[labels == 1].shape # print("label 1 : {}".format(num_tmp)) # num_tmp = labels[labels == 2].shape # print("label 2 : {}".format(num_tmp)) # confus_matrix = tf.contrib.metrics.confusion_matrix(labels, predictions, n_class) confus_matrix = tf.contrib.metrics.confusion_matrix( valid_pred, valid_ref, n_class) with tf.Session() as sess: confus_matrix = sess.run(confus_matrix) print(confus_matrix) confus_matrix = np.array(confus_matrix) oa = 0 x_row_plus = [] x_col_plus = [] x_diagonal = [] for i in range(n_class): oa += confus_matrix[i, i] x_diagonal.append(confus_matrix[i, i]) row_sum = sum(confus_matrix[i, :]) col_sum = sum(confus_matrix[:, i]) x_row_plus.append(row_sum) x_col_plus.append(col_sum) print("x_row_plus:{}".format(x_row_plus)) print("x_col_plus:{}".format(x_col_plus)) x_row_plus = np.array(x_row_plus) x_col_plus = np.array(x_col_plus) x_diagonal = np.array(x_diagonal) x_total = sum(x_row_plus) OA_acc = oa / (sum(x_row_plus)) print("\nOA:{:.3f}".format(OA_acc)) tmp = x_col_plus * x_row_plus kappa = (x_total * sum(x_diagonal) - sum(x_col_plus * x_row_plus) ) / np.float(x_total * x_total - sum(x_col_plus * x_row_plus)) print("Kappa:{:.3f}".format(kappa)) for i in range(n_class - 1): i = i + 1 prec = x_diagonal[i] / x_row_plus[i] print("\nForground of {}_accuracy= {:.3f}".format(i, prec)) recall = x_diagonal[i] / x_col_plus[i] print("{}_recall= {:.3f}".format(i, recall)) iou = x_diagonal[i] / (x_row_plus[i] + x_col_plus[i] - x_diagonal[i]) print("{}_iou {:.3f}".format(i, iou))
'../../data/models/sat_urban_4bands/', dict_network[FLAG_USING_NETWORK], '_', dict_target[FLAG_TARGET_CLASS], '_binary_onlyjaccard_final.h5' ]) # model_file = '/home/omnisky/PycharmProjects/data/models/sat_urban_4bands/unet_buildings_binary_onlyjaccard_2018-09-29_18-55-11.h5' print("model: {}".format(model_file)) if __name__ == '__main__': print("[INFO] opening image...") # ret, input_img = load_img_normalization_by_cv2(img_file) # if ret !=0: # print("Open input file failed: {}".format(img_file)) # sys.exit(-1) input_img = load_img_by_gdal(img_file) if im_type == UINT8: input_img = input_img / 255.0 elif im_type == UINT10: input_img = input_img / 1024.0 elif im_type == UINT16: input_img = input_img / 65535.0 input_img = np.clip(input_img, 0.0, 1.0) abs_filename = os.path.split(img_file)[1] abs_filename = abs_filename.split(".")[0] print(abs_filename) """checke model file""" print("model file: {}".format(model_file)) if not os.path.isfile(model_file):
def predict_multiclass_for_single_image(input_dict={}): gup_id = input_dict['GPUID'] os.environ["CUDA_VISIBLE_DEVICES"] = gup_id window_size = input_dict['windsize'] im_bands = input_dict['im_bands'] im_type = input_dict['dtype'] FLAG_APPROACH_PREDICT = input_dict['strategy'] img_file = input_dict['image_file'] model_file = input_dict['model_file'] output_dir = input_dict['mask_dir'] out_bands = input_dict['target_num'] input_img = load_img_by_gdal(img_file) if im_type == UINT8: input_img = input_img / 255.0 elif im_type == UINT10: input_img = input_img / 1024.0 elif im_type == UINT16: input_img = input_img / 65535.0 input_img = np.clip(input_img, 0.0, 1.0) input_img = input_img.astype(np.float16) # test accuracy abs_filename = os.path.split(img_file)[1] abs_filename = abs_filename.split(".")[0] print(abs_filename) """checke model file""" print("model file: {}".format(model_file)) if not os.path.isfile(model_file): print("model does not exist:{}".format(model_file)) sys.exit(-2) model = load_model(model_file) if FLAG_APPROACH_PREDICT == 0: print("[INFO] predict image by orignal approach\n") result = orignal_predict_onehot(input_img, im_bands, model, window_size) output_file = ''.join([output_dir, '/', abs_filename, '.png']) print("result save as to: {}".format(output_file)) cv2.imwrite(output_file, result) # # for b in range(out_bands): # output_file = ''.join([output_dir, '/', abs_filename, '_', dict_target[b], '.png']) # print("result save as to: {}".format(output_file)) # cv2.imwrite(output_file, result[:, :, b]) elif FLAG_APPROACH_PREDICT == 1: print("[INFO] predict image by smooth approach\n") result = predict_img_with_smooth_windowing_multiclassbands( input_img, model, window_size=window_size, subdivisions=2, real_classes=out_bands, # output channels = 是真的类别,总类别-背景 pred_func=smooth_predict_for_multiclass) H, W, C = np.array(input_img).shape output_file = ''.join( [output_dir, '/', abs_filename, '_smooth_pred.png']) output_mask = np.zeros((H, W), np.uint8) for i in range(out_bands): indx = np.where(result[:, :, i] >= 127) output_mask[indx] = i + 1 print(np.unique(result)) cv2.imwrite(output_file, output_mask) print("Saved to:{}".format(output_file)) # for b in range(out_bands): # output_file = ''.join([output_dir,'/', abs_filename, '_', dict_target[b], '.png']) # print("result save as to: {}".format(output_file)) # cv2.imwrite(output_file, result[:,:,b]) gc.collect() return 0