def create_patches( sat_patch_size, map_patch_size, stride, map_ch, sat_data_dir, map_data_dir, sat_out_dir, map_out_dir ): if os.path.exists(sat_out_dir): shutil.rmtree(sat_out_dir) if os.path.exists(map_out_dir): shutil.rmtree(map_out_dir) os.makedirs(sat_out_dir) os.makedirs(map_out_dir) # db sat_env = lmdb.Environment(sat_out_dir, map_size=1099511627776) sat_txn = sat_env.begin(write=True, buffers=False) map_env = lmdb.Environment(map_out_dir, map_size=1099511627776) map_txn = map_env.begin(write=True, buffers=False) # patch size sat_size = sat_patch_size map_size = map_patch_size print("patch size:", sat_size, map_size, stride) # get filenames sat_fns = np.asarray(sorted(glob.glob("%s/*.tif*" % sat_data_dir))) map_fns = np.asarray(sorted(glob.glob("%s/*.tif*" % map_data_dir))) index = np.arange(len(sat_fns)) np.random.shuffle(index) sat_fns = sat_fns[index] map_fns = map_fns[index] # create keys keys = np.arange(15000000) np.random.shuffle(keys) n_all_files = len(sat_fns) print("n_all_files:", n_all_files) n_patches = 0 for file_i, (sat_fn, map_fn) in enumerate(zip(sat_fns, map_fns)): if (os.path.basename(sat_fn).split(".")[0]) != (os.path.basename(map_fn).split(".")[0]): print("File names are different", sat_fn, map_fn) return sat_im = cv.imread(sat_fn, cv.IMREAD_COLOR) map_im = cv.imread(map_fn, cv.IMREAD_GRAYSCALE) map_im = map_im[:, :, np.newaxis] st = time.time() sat_patches, map_patches = divide_to_patches(stride, sat_size, map_size, sat_im, map_im) print("divide:{}".format(time.time() - st)) sat_patches = np.asarray(sat_patches, dtype=np.uint8) map_patches = np.asarray(map_patches, dtype=np.uint8) for patch_i in range(sat_patches.shape[0]): sat_patch = sat_patches[patch_i] map_patch = map_patches[patch_i] key = b"%010d" % keys[n_patches] sat_txn.put(key, sat_patch.tobytes()) map_txn.put(key, map_patch.tobytes()) n_patches += 1 print(file_i, "/", n_all_files, "n_patches:", n_patches) sat_txn.commit() sat_env.close() map_txn.commit() map_env.close() print("patches:\t", n_patches)
def create_patches(sat_patch_size, map_patch_size, stride, map_ch, sat_data_dir, map_data_dir, sat_out_dir, map_out_dir): if os.path.exists(sat_out_dir): shutil.rmtree(sat_out_dir) if os.path.exists(map_out_dir): shutil.rmtree(map_out_dir) os.makedirs(sat_out_dir) os.makedirs(map_out_dir) # db sat_env = lmdb.Environment(sat_out_dir, map_size=1099511627776) sat_txn = sat_env.begin(write=True, buffers=False) map_env = lmdb.Environment(map_out_dir, map_size=1099511627776) map_txn = map_env.begin(write=True, buffers=False) # patch size sat_size = sat_patch_size map_size = map_patch_size print('patch size:', sat_size, map_size, stride) # get filenames sat_fns = np.asarray(sorted(glob.glob('%s/*.tif*' % sat_data_dir))) map_fns = np.asarray(sorted(glob.glob('%s/*.tif*' % map_data_dir))) index = np.arange(len(sat_fns)) np.random.shuffle(index) sat_fns = sat_fns[index] map_fns = map_fns[index] # create keys keys = np.arange(15000000) np.random.shuffle(keys) n_all_files = len(sat_fns) print('n_all_files:', n_all_files) n_patches = 0 for file_i, (sat_fn, map_fn) in enumerate(zip(sat_fns, map_fns)): if ((os.path.basename(sat_fn).split('.')[0]) != (os.path.basename(map_fn).split('.')[0])): print('File names are different', sat_fn, map_fn) return sat_im = cv.imread(sat_fn, cv.IMREAD_COLOR) map_im = cv.imread(map_fn, cv.IMREAD_GRAYSCALE) map_im = map_im[:, :, np.newaxis] st = time.time() sat_patches, map_patches = divide_to_patches(stride, sat_size, map_size, sat_im, map_im) print('divide:{}'.format(time.time() - st)) sat_patches = np.asarray(sat_patches, dtype=np.uint8) map_patches = np.asarray(map_patches, dtype=np.uint8) for patch_i in range(sat_patches.shape[0]): sat_patch = sat_patches[patch_i] map_patch = map_patches[patch_i] key = b'%010d' % keys[n_patches] sat_txn.put(key, sat_patch.tobytes()) map_txn.put(key, map_patch.tobytes()) n_patches += 1 print(file_i, '/', n_all_files, 'n_patches:', n_patches) sat_txn.commit() sat_env.close() map_txn.commit() map_env.close() print('patches:\t', n_patches)
import time import numpy as np import cv2 as cv sat_im = cv.imread('data/mass_buildings/test/sat/22828930_15.tiff', cv.IMREAD_COLOR) map_im = cv.imread('data/mass_buildings/test/map/22828930_15.tif', cv.IMREAD_GRAYSCALE)[:, :, np.newaxis] print(sat_im.shape, map_im.shape) stride = 16 sat_size = 92 map_size = 24 st = time.time() sat_patches, map_patches = divide_to_patches(stride, sat_size, map_size, sat_im, map_im) print(time.time() - st) st = time.time() sat_patches, map_patches = divide_to_patches(stride, sat_size, map_size, sat_im, map_im) print(time.time() - st) out_dir = 'data/test_patches' if not os.path.exists(out_dir): os.mkdir(out_dir) for i in range(sat_patches.shape[0]): sp = sat_patches[i] mp = map_patches[i][:, :, 0] mp = np.array([mp == 0, mp == 1, mp == 2], dtype=np.uint8) * 255 mp = mp.transpose((1, 2, 0))
import time import numpy as np import cv2 as cv sat_im = cv.imread('data/mass_buildings/test/sat/22828930_15.tiff', cv.IMREAD_COLOR) map_im = cv.imread('data/mass_buildings/test/map/22828930_15.tif', cv.IMREAD_GRAYSCALE)[:, :, np.newaxis] print(sat_im.shape, map_im.shape) stride = 16 sat_size = 92 map_size = 24 st = time.time() sat_patches, map_patches = divide_to_patches( stride, sat_size, map_size, sat_im, map_im) print(time.time() - st) st = time.time() sat_patches, map_patches = divide_to_patches( stride, sat_size, map_size, sat_im, map_im) print(time.time() - st) out_dir = 'data/test_patches' if not os.path.exists(out_dir): os.mkdir(out_dir) for i in range(sat_patches.shape[0]): sp = sat_patches[i] mp = map_patches[i][:, :, 0] mp = np.array([mp == 0, mp == 1, mp == 2], dtype=np.uint8) * 255 mp = mp.transpose((1, 2, 0))