def convert_mat_to_png(mat_folder, b_runmatlab=True): # need to run matlab script # original_path = str(os.getcwd()) # path = os.path.join(codes_path,'DeepLab-Context','matlab','my_script') # os.chdir(path) # # convert the mat files to png or tif # if b_runmatlab : # subprocess.call("matlab -r 'converttoPng; exit;'", shell=True) # os.chdir(original_path) # using python script instead of matlab script result = mat_To_png.convert_mat_to_png(mat_folder) if result is False: return False # read the png or tif files list # result = io_function.get_file_list_by_ext('.tif',mat_file_foler,bsub_folder=False) # result = io_function.get_file_list_by_ext('.mat', mat_file_foler, bsub_folder=False) # if len(result) < 1: # result = io_function.get_file_list_by_ext('.png', mat_file_foler, bsub_folder=False) # if len(result) < 1: # basic.outputlogMessage('error, Not result (.tif or .png) in Mat folder:%s'%mat_file_foler) # return False if len(result) != len(test_data): basic.outputlogMessage( 'error, the count of results is not the same as input test file') return False return result
def model_finder(path): mtime = lambda f: os.stat(os.path.join(path, f)).st_mtime files = list(sorted(glob.glob(path + '/*.pth'), key=mtime)) if len(files) >= 1: file_ = files[-1] else: basic.outputlogMessage('error, can not find pth model file') return False return file_
def run_train(): if len(train_data)<1: basic.outputlogMessage('error, No input image for training') return False init_model_path ='data/MS_DeepLab_resnet_pretrained_COCO_init.pth' train.run_training(gpuid,init_model_path,train_data,batch_size=1) pass
def merge_edge_to_detected_result(edgemap_list, detected_png_list): if io_function.is_file_exist(edgemap_list) is False: return False edge_data = [] f_obj = open(edgemap_list) f_lines = f_obj.readlines() f_obj.close() for i in range(0, len(f_lines)): temp = f_lines[i].split() sample = SampleClass() sample.image = temp[0] sample.edge_map = temp[1] edge_data.append(sample) if len(edge_data) != len(detected_png_list): basic.outputlogMessage( 'error, the count of edge map is different from the one of detected result' ) return False # make sure edge_data have the same order of png list for i in range(0, len(edge_data)): if edge_data[i].image != test_data[i].image: basic.outputlogMessage( 'error, the source data for edge and building detection are different' ) return False #merge two png (edge and detected) edge_file = edge_data[i].edge_map im_edge = Image.open(edge_file) in_edge = numpy.array(im_edge, dtype=numpy.uint8) det_file = os.path.join( os.path.split(detected_png_list[i])[0], test_data[i].id + '_blob_0.png') if det_file not in detected_png_list: basic.outputlogMessage('result_file file not in the list %s' % det_file) return False im_det = Image.open(det_file) in_det = numpy.array(im_det, dtype=numpy.uint8) in_det[in_edge == 0] = 0 # 0 is the value of edge pixel #backup for test backcup_name = io_function.get_name_by_adding_tail(det_file, 'bak') io_function.copy_file_to_dst(det_file, backcup_name) basic.outputlogMessage('merge edge to detection result: %d /%d: %s to %s' % (i + 1, len(edge_data),\ os.path.basename(edge_file),os.path.basename(det_file))) #over write original image # cv2.imwrite(det_file, in_det) misc.imsave(det_file, in_det) return True
def run_test(): """ run test with pytorch_deeplab_resnet :return: result file list """ if len(test_data) < 1: basic.outputlogMessage('error, no input data in the list ') return False find_model = model_finder(os.path.join(expr_folder, 'data', 'snapshots')) if find_model is False: return False result_list = evalpyt.run_evalpyt(gpuid, find_model, test_data, save_file_folder) return result_list
def test_plot_truth_coords(): import SpaceNetData.plot_truth_coords as ptcoords import SpaceNetData.geojson_to_pixel_arr as geojson_to_pixel_arr img_path = "/Users/huanglingcao/Data/aws_SpaceNet/AOI_3_Paris_Train/RGB-PanSharpen/RGB-PanSharpen_AOI_3_Paris_img183.tif" # img_path = "/Users/huanglingcao/Data/aws_SpaceNet/AOI_3_Paris_Train/PAN/PAN_AOI_3_Paris_img183.tif" geojson = '/Users/huanglingcao/Data/aws_SpaceNet/AOI_3_Paris_Train/geojson/buildings/buildings_AOI_3_Paris_img183.geojson' pixel_coords, latlon_coords = \ geojson_to_pixel_arr.geojson_to_pixel_arr(img_path,geojson,pixel_ints=True,verbose=True) # img_obj = RSImageclass() # if img_obj.open(img_path) is False: # basic.outputlogMessage('Open image %s failed'%img_path) # return False # Datatype = img_obj.GetGDALDataType() # width = img_obj.GetWidth() # height = img_obj.GetHeight() img_pro_obj = RSImgProclass() input_image_band1 =img_pro_obj.Read_Image_band_data_to_numpy_array_all_pixel(1,img_path) print numpy.min(input_image_band1), numpy.max(input_image_band1) input_image_band1 = (input_image_band1- numpy.min(input_image_band1))*1.0/(numpy.max(input_image_band1) - numpy.min(input_image_band1)) print numpy.min(input_image_band1),numpy.max(input_image_band1) print input_image_band1.shape input_image_band2 = img_pro_obj.Read_Image_band_data_to_numpy_array_all_pixel(2, img_path) input_image_band2 = (input_image_band2 - numpy.min(input_image_band2)) * 1.0 / (numpy.max(input_image_band2) - numpy.min(input_image_band2)) print numpy.min(input_image_band2), numpy.max(input_image_band2) print input_image_band2.shape input_image_band3 = img_pro_obj.Read_Image_band_data_to_numpy_array_all_pixel(3, img_path) input_image_band3 = (input_image_band3 - numpy.min(input_image_band3)) * 1.0 / (numpy.max(input_image_band3) - numpy.min(input_image_band3)) print numpy.min(input_image_band3), numpy.max(input_image_band3) print input_image_band3.shape if input_image_band1 is False: basic.outputlogMessage('Open image %s failed' % img_path) return False input_image = numpy.stack((input_image_band1,input_image_band2,input_image_band3),axis=2) # input_image = input_image_band1 # input_image.reshape(650,650,3) input_image = input_image.astype(numpy.float32) # the value of each pixel is between 0-1 print input_image.shape ptcoords.plot_truth_coords(input_image,pixel_coords) pass
def read_test_data(test_file, file_id): if os.path.isfile(test_file) is False: basic.outputlogMessage('error: file not exist %s' % test_file) return False f_obj = open(test_file) fid_obj = open(file_id) f_lines = f_obj.readlines() fid_lines = fid_obj.readlines() fid_obj.close() f_obj.close() if len(f_lines) != len(fid_lines): basic.outputlogMessage( 'the number of lines in test_file and test_file_id is not the same' ) return False for i in range(0, len(f_lines)): temp = f_lines[i].split() # temp[1]. temp.append(fid_lines[i].strip()) test_data.append(temp) return True
def combine_IMGpath_and_Groundpath(img_only_list_file, img_groud_true_list_file, result_path): if io_function.is_file_exist(img_only_list_file) is False \ or io_function.is_file_exist(img_groud_true_list_file) is False: return False f_img_only = open(img_only_list_file, 'r') f_img_ground = open(img_groud_true_list_file, 'r') fw_obj = open(result_path, 'w') mg_ground_lines = f_img_ground.readlines() for line in f_img_only.readlines(): line = line.strip() # get imgid imgid = os.path.splitext(os.path.basename(line))[0] imgid = imgid[imgid.find('img'):len(imgid)] write_line = '' for save_line in mg_ground_lines: bfound = False if save_line.find(imgid + 'segcls.png') >= 0: temp = save_line.split() write_line = line + ' ' + temp[1] + '\n' bfound = True break if bfound: fw_obj.writelines(write_line) else: basic.outputlogMessage('Image %s can not found ground True' % line) f_img_only.close() f_img_ground.close() fw_obj.close() return True
def read_test_data(test_file, file_id=None): """ read the file list :param test_file: file tontains the test file list :param file_id: id is need in caffe for output result, not need in pytorch_deeplab_resnet :return: True if succeful, False otherwise """ if os.path.isfile(test_file) is False: basic.outputlogMessage('error: file not exist %s' % test_file) return False f_obj = open(test_file) f_lines = f_obj.readlines() f_obj.close() if file_id is not None: fid_obj = open(file_id) fid_lines = fid_obj.readlines() fid_obj.close() if len(f_lines) != len(fid_lines): basic.outputlogMessage( 'the number of lines in test_file and test_file_id is not the same' ) return False for i in range(0, len(f_lines)): temp = f_lines[i].split() sample = SampleClass() sample.image = temp[0] if len(temp) > 1: sample.groudT = temp[1] sample.id = fid_lines[i].strip() test_data.append(sample) else: for i in range(0, len(f_lines)): temp = f_lines[i].split() sample = SampleClass() sample.image = temp[0] if len(temp) > 1: sample.groudT = temp[1] test_data.append(sample) # prepare file for pytorch_deeplab_resnet if len(test_data) < 1: basic.outputlogMessage('error, not input test data ') return False # check all image file and ground true file for sample in test_data: # check image path image_basename = os.path.basename(sample.image) if os.path.isfile(sample.image) is False: # sample.image = os.path.basename(sample.image) sample.image = os.path.join(IMpath, image_basename) if os.path.isfile(sample.image) is False: basic.outputlogMessage('error, file not exist: %s' % sample.image) return False # check ground path if len(sample.groudT) > 0 and os.path.isfile(sample.groudT) is False: sample.groudT = os.path.basename(sample.groudT) sample.groudT = os.path.join(GTpath, sample.groudT) if len(sample.id) < 1: sample.id = os.path.splitext(image_basename)[0] basic.outputlogMessage('read test data completed, sample count %d' % len(test_data)) return True
def convert_png_result_to_geojson(result_list): rsimg_obj = RSImageclass() rsimgPro_obj = RSImgProclass() if len(result_list) != len(test_data): basic.outputlogMessage( 'error, the count of results is not the same as input test file') return False geojson_without_fix_folder = os.path.join(save_file_folder, 'geojson_without_fix') io_function.mkdir(geojson_without_fix_folder) geojson_folder = os.path.join(save_file_folder, 'geojson') io_function.mkdir(geojson_folder) basic.outputlogMessage('geojson_without_fix_folder: %s' % geojson_without_fix_folder) basic.outputlogMessage('geojson_folder: %s' % geojson_folder) geojson_list = [] for i in range(0, len(result_list)): # read geo information if rsimg_obj.open(test_data[i].image) is False: return False result_file = os.path.join(save_file_folder, test_data[i].id + '.png') if result_file not in result_list: basic.outputlogMessage('result_file file not in the list %s' % result_file) return False basic.outputlogMessage( 'png to geojson: %d / %d :Convert file: %s' % (i + 1, len(result_list), os.path.basename(result_file))) prj = rsimg_obj.GetProjection() geom = rsimg_obj.GetGeoTransform() # read pixel value im_data = rsimgPro_obj.Read_Image_band_data_to_numpy_array_all_pixel( 1, result_file) if im_data is False: basic.outputlogMessage('Read image data failed: %s' % result_file) return False # im = Image.open(result_file) # Can be many different formats. # pix = im.load() # im_data = numpy.array(pix) imgID = test_data[i].id #test_data[i][2] geojson = geoJSONfromCluster.CreateGeoJSON(geojson_without_fix_folder, imgID, im_data, geom, prj) fix_geojson = FixGeoJSON.FixGeoJSON(geojson, geojson_folder) geojson_list.append(fix_geojson) return geojson_list
def read_test_data(test_file, file_id): if os.path.isfile(test_file) is False: basic.outputlogMessage('error: file not exist %s' % test_file) return False f_obj = open(test_file) f_lines = f_obj.readlines() f_obj.close() if file_id is not None: fid_obj = open(file_id) fid_lines = fid_obj.readlines() fid_obj.close() if len(f_lines) != len(fid_lines): basic.outputlogMessage( 'the number of lines in test_file and test_file_id is not the same' ) return False for i in range(0, len(f_lines)): temp = f_lines[i].split() sample = SampleClass() sample.image = temp[0] if len(temp) > 1: sample.groudT = temp[1] sample.id = fid_lines[i].strip() test_data.append(sample) else: for i in range(0, len(f_lines)): temp = f_lines[i].split() sample = SampleClass() sample.image = temp[0] if len(temp) > 1: sample.groudT = temp[1] test_data.append(sample) # prepare file for pytorch_deeplab_resnet if len(test_data) < 1: basic.outputlogMessage('error, not input test data ') return False # check all image file and ground true file for sample in test_data: # check image path image_basename = os.path.basename(sample.image) if os.path.isfile(sample.image) is False: basic.outputlogMessage('error, file not exist: %s' % sample.image) return False # check ground path if len(sample.groudT) > 0 and os.path.isfile(sample.groudT) is False: basic.outputlogMessage('error, file not exist: %s' % sample.groudT) return False # if len(sample.id)< 1: # sample.id = os.path.splitext(image_basename)[0] basic.outputlogMessage('read test data completed, sample count %d' % len(test_data)) return True
import basic.basic as basic import basic.io_function as io_function from basic.RSImage import RSImageclass from basic.RSImageProcess import RSImgProclass import basic.mat_To_png as mat_To_png # sys.path.insert(0, codes_path + '/SpaceNetData') import SpaceNetData.geoJSONfromCluster as geoJSONfromCluster import SpaceNetData.FixGeoJSON as FixGeoJSON #sys.path.insert(0, os.getcwd() + '../SpaceNetChallenge/') import SpaceNetChallenge.utilities.python.createCSVFromGEOJSON as createCSVFromGEOJSON if len(sys.argv) == 2: expr = sys.argv[1] basic.outputlogMessage('set expr as : %s' % expr) if len(sys.argv) == 3: expr = sys.argv[1] gpuid = int(sys.argv[2]) basic.outputlogMessage('set expr as : %s' % expr) basic.outputlogMessage('set gpuid as : %d' % gpuid) if os.path.isdir(expr) is False: print 'error, %s not exist ' % expr exit(1) run_deeplab.EXP = expr run_deeplab.DEV_ID = gpuid run_deeplab.NET_ID = NET_ID test_file = expr + '/list/val.txt'
def run_evalpyt(gpu0, model_path, test_data, save_dir): model = getattr(deeplab_resnet, 'Res_Deeplab')() model.eval() model.cuda(gpu0) img_rows = 650 img_cols = 650 band = 3 # model_path = os.path.join('data/snapshots/', snapPrefix + str(iter) + '000.pth') basic.outputlogMessage('the trained model file used in this test is: %s' % model_path) saved_state_dict = torch.load(model_path) model.load_state_dict(saved_state_dict) save_png_dir = save_dir if os.path.isdir(save_png_dir) is False: io_function.mkdir(save_png_dir) pytorch_list = [] result_list = [] count = 0 for test_sample in test_data: img = np.zeros((663, 663, band)) count = count + 1 img_path = test_sample.image # img_temp = cv2.imread(os.path.join(im_path,i[:-1]+'.jpg')).astype(float) img_temp = cv2.imread(img_path).astype(float) img_original = img_temp img_temp[:, :, 0] = img_temp[:, :, 0] - 104.008 img_temp[:, :, 1] = img_temp[:, :, 1] - 116.669 img_temp[:, :, 2] = img_temp[:, :, 2] - 122.675 img[:img_temp.shape[0], :img_temp.shape[1], :] = img_temp # gt = cv2.imread(os.path.join(gt_path,i[:-1]+'.png'),0) output = model( Variable(torch.from_numpy(img[np.newaxis, :].transpose(0, 3, 1, 2)).float(), volatile=True).cuda(gpu0)) interp = nn.UpsamplingBilinear2d(size=(663, 663)) output = interp(output[3]).cpu().data[0].numpy() output = output[:, :img_temp.shape[0], :img_temp.shape[1]] output = output.transpose(1, 2, 0) output = np.argmax(output, axis=2) # save to png result = np.uint8(output) result = result.reshape(img_rows, img_cols) result[np.where(result == 1)] = 255 save_png = os.path.join(save_png_dir, test_sample.id + '.png') misc.imsave(save_png, result) result_list.append(save_png) basic.outputlogMessage('%d/%d : saved %s' % (count, len(test_data), save_png)) # if args['--visualize']: # plt.subplot(3, 1, 1) # plt.imshow(img_original) # plt.subplot(3, 1, 2) # plt.imshow(gt) # plt.subplot(3, 1, 3) # plt.imshow(output) # plt.show() png_path = test_sample.groudT if len(png_path) > 0: gt = cv2.imread(png_path, 0) gt[gt == 255] = 0 iou_pytorch = get_iou(output, gt) pytorch_list.append(iou_pytorch) if len(pytorch_list) > 0: print('pytorch', iter, np.sum(np.asarray(pytorch_list)) / len(pytorch_list)) basic.outputlogMessage('pytorch {} , {} \n'.format( iter, np.sum(np.asarray(pytorch_list)) / len(pytorch_list))) return result_list