def readFromDiskIteratorTrain(self, image_dir, chipFiles, batch_size, patch_size, padding=(0, 0), dataAug=''): assert batch_size == 1 # this is a iterator for training nDims = len(chipFiles[0]) while True: image_batch = np.zeros((4, patch_size[0], patch_size[1], nDims)) # select number to sample idx_batch = np.random.permutation(len(chipFiles)) for cnt, randInd in enumerate(idx_batch): row = chipFiles[randInd] blockList = [] nDims = 0 for file in row: img = util_functions.uabUtilAllTypeLoad( os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList).astype(np.float32) if self.block_mean is not None: block -= self.block_mean if dataAug != '': augDat = uabUtilreader.doDataAug(block, nDims, dataAug, is_np=True, img_mean=self.block_mean) else: augDat = block if (np.array(padding) > 0).any(): augDat = uabUtilreader.pad_block(augDat, padding) image_batch[0, :, :, :] = augDat for i in range(1, 4): image_batch[i, :, :, :] = np.rot90(augDat, k=i, axes=(0, 1)) if (cnt + 1) % batch_size == 0: yield image_batch[:, :, :, 1:], image_batch[:, :, :, :1]
def readFromDiskIteratorTrain(self, image_dir, chipFiles, batch_size, patch_size, padding=(0, 0), dataAug=''): # this is a iterator for training # pure random idx = np.random.permutation(len(chipFiles)) nDims = len(chipFiles[0]) while True: image_batch = np.zeros( (batch_size, patch_size[0], patch_size[1], nDims)) cityid_batch = np.zeros(batch_size, dtype=np.uint8) for cnt, randInd in enumerate(idx): row = chipFiles[randInd] blockList = [] nDims = 0 city_name = ''.join( [a for a in row[0].split('_')[0] if not a.isdigit()]) cityid_batch[cnt % batch_size] = self.city_dict[city_name] for file in row: img = util_functions.uabUtilAllTypeLoad( os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList).astype(np.float32) if self.block_mean is not None: block -= self.block_mean if dataAug != '': augDat = uabUtilreader.doDataAug(block, nDims, dataAug, is_np=True) else: augDat = block if (np.array(padding) > 0).any(): augDat = uabUtilreader.pad_block(augDat, padding) image_batch[cnt % batch_size, :, :, :] = augDat if ((cnt + 1) % batch_size == 0): yield image_batch[:, :, :, 1:], image_batch[:, :, :, : 1], cityid_batch
def readFromDiskIteratorTest(self, image_dir, chipFiles, batch_size, tile_dim, patch_size, overlap=0, padding=(0, 0)): # this is a iterator for test for row in chipFiles: blockList = [] nDims = 0 for cnt, file in enumerate(row): if type(image_dir) is list: img = util_functions.uabUtilAllTypeLoad( os.path.join(image_dir[cnt], file)) else: img = util_functions.uabUtilAllTypeLoad( os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList).astype(np.float32) if self.block_mean is not None: block -= self.block_mean if (np.array(padding) > 0).any(): block = uabUtilreader.pad_block(block, padding) tile_dim = tile_dim + padding * 2 block = self._aug(block) ind = 0 image_batch = np.zeros( (batch_size, patch_size[0], patch_size[1], nDims)) for patch in uabUtilreader.patchify(block, tile_dim, patch_size, overlap=overlap): # print(str(ind) +': '+ str(patch.shape)) image_batch[ind, :, :, :] = patch ind += 1 if ind == batch_size: ind = 0 yield image_batch # yield the last chunk if ind > 0: yield image_batch[:ind, :, :, :]
def readFromDiskIteratorTrain(self, image_dir, chipFiles, batch_size, patch_size, random, padding=(0, 0), dataAug=''): # this is a iterator for training if (random): idx = np.random.permutation(len(chipFiles)) else: idx = np.arange(stop=len(chipFiles)) nDims = len(chipFiles[0]) while True: image_batch = np.zeros( (batch_size, patch_size[0], patch_size[1], nDims)) for cnt, randInd in enumerate(idx): row = chipFiles[randInd] blockList = [] nDims = 0 for file in row: img = util_functions.uabUtilAllTypeLoad( os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList) if self.block_mean is not None: block -= self.block_mean if dataAug != '': augDat = uabUtilreader.doDataAug(block, nDims, dataAug, img_mean=self.block_mean, is_np=True) else: augDat = block if (np.array(padding) > 0).any(): augDat = uabUtilreader.pad_block(augDat, padding) image_batch[cnt % batch_size, :, :, :] = augDat if ((cnt + 1) % batch_size == 0): yield image_batch[:, :, :, 1:], image_batch[:, :, :, :1]
def readFromDiskIteratorTrain(self, image_dir, chipFiles, batch_size, patch_size, padding=(0, 0), dataAug=''): # this is a iterator for training nDims = len(chipFiles[0]) while True: image_batch = np.zeros((batch_size, self.center_crop[0], self.center_crop[1], nDims)) building_truth = np.zeros((batch_size, 2)) # select number to sample idx_batch = np.random.permutation(len(chipFiles)) for cnt, randInd in enumerate(idx_batch): row = chipFiles[randInd] blockList = [] nDims = 0 for file in row: img = util_functions.uabUtilAllTypeLoad(os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList).astype(np.float32) if self.block_mean is not None: block -= self.block_mean if dataAug != '': augDat = uabUtilreader.doDataAug(block, nDims, dataAug, is_np=True, img_mean=self.block_mean) else: augDat = block if (np.array(padding) > 0).any(): augDat = uabUtilreader.pad_block(augDat, padding) augDat = util_functions.crop_center(augDat, self.center_crop[0], self.center_crop[1]) store_idx = cnt % batch_size image_batch[store_idx, :, :, :] = augDat percent = np.sum(augDat[:, :, 0]) / (self.center_crop[0] * self.center_crop[1]) if percent > self.patch_prob: building_truth[store_idx, :] = [0, 1] else: building_truth[store_idx, :] = [1, 0] if (cnt + 1) % batch_size == 0: yield (image_batch[:, :, :, 1:], building_truth)
def readFromDiskIteratorTest(self, image_dir, chipFiles, batch_size, tile_dim, patch_size, overlap=0, padding=(0, 0)): # this is a iterator for test for row in chipFiles: blockList = [] nDims = 0 for cnt, file in enumerate(row): if type(image_dir) is list: img = util_functions.uabUtilAllTypeLoad(os.path.join(image_dir[cnt], file)) else: img = util_functions.uabUtilAllTypeLoad(os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList).astype(np.float32) if not np.all([np.array(tile_dim) == block.shape[:2]]): block = skimage.transform.resize(block, tile_dim, order=0, preserve_range=True, mode='reflect') if self.block_mean is not None: block -= self.block_mean if (np.array(padding) > 0).any(): block = uabUtilreader.pad_block(block, padding) tile_dim = tile_dim + padding * 2 ind = 0 image_batch = np.zeros((batch_size, patch_size[0], patch_size[1], nDims)) for patch in uabUtilreader.patchify(block, tile_dim, patch_size, overlap=overlap): patch_gt = (patch[:, :, 0] > 0).astype(np.uint8) patch[:, :, 0] = get_lines(patch_gt, np.array(patch_size)) image_batch[ind, :, :, :] = patch ind += 1 if ind == batch_size: ind = 0 yield image_batch # yield the last chunk if ind > 0: yield image_batch[:ind, :, :, :]
rManager = reader.readManager # run the model with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) model.load(model_dir, sess) model.model_name = model_dir.split('/')[-1] result = model.test('X', sess, rManager) # patchify gt gt_img = np.expand_dims(imageio.imread( os.path.join(parent_dir_truth, file_name_truth)), axis=-1) gt_img = uabUtilreader.pad_block( gt_img, np.array( (model.get_overlap() / 2, model.get_overlap() / 2))) gtReader = uabUtilreader.patchify( gt_img, tile_size + np.array((model.get_overlap(), model.get_overlap())), input_size, overlap=model.get_overlap()) patch_num = result.shape[0] for n in range(patch_num): pred = np.argmax(result[n, :, :, :], axis=2).astype(np.uint8) pred_gt = next(gtReader)[:, :, 0] * 255 try: imageio.imsave( os.path.join(save_dir,
idx, file_list = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt', 'city') file_list_valid = uabCrossValMaker.make_file_list_by_key(idx, file_list, [0]) building_percent = np.zeros(len(file_list_valid)) for cnt, files in enumerate(tqdm(file_list_valid)): gt_file_name = files[-1] gt_parts = gt_file_name.split('_') city_id = int(''.join([i for i in gt_parts[0] if i.isdigit()])) y_str, x_str = gt_parts[1].split('x') y = int(''.join([i for i in y_str if i.isdigit()])) x = int(''.join([i for i in x_str if i.isdigit()])) pred_file_name = os.path.join( pred_file_dir, '{}{}.png'.format(city_list[city_num], city_id)) pred = imageio.imread(pred_file_name) if pad > 0: pred = np.squeeze(uabUtilreader.pad_block(np.expand_dims(pred, axis=2), np.array([pad, pad])), axis=2) patch = pred[y:y + c_size, x:x + c_size] if pad > 0: patch = center_crop(patch, (pad, pad)) building_percent[cnt] = np.sum(patch) / (size * size) np.save( os.path.join( task_dir, '{}_{}_building_record.npy'.format(cnn_name, city_list[city_num])), building_percent)
pred_dir = r'/hdd/Results/domain_selection/UnetPredict_inria_loo_mtl_cust_{}_0_PS(572, 572)_BS5_' \ r'EP100_LR0.0001_DS60_DR0.1_SFN32/inria_all/pred'.format(city_num) if model_name == 'unet': patch_size = (572, 572) overlap = 92 pad = 92 else: patch_size = (321, 321) overlap = 0 pad = 0 # extract patches tile_files = sorted(glob(os.path.join(pred_dir, '{}*.png'.format(city_list[city_num])))) pred_building_binary = [] for file in tile_files: gt = imageio.imread(file) gt = np.expand_dims(gt, axis=2) gt = uabUtilreader.pad_block(gt, np.array([pad, pad])) for patch in uabUtilreader.patchify(gt, (5184, 5184), patch_size, overlap): if model_name == 'deeplab': pred_raw = np.sum(patch) / (patch_size[0] * patch_size[1]) else: pred_raw = np.sum(util_functions.crop_center(patch, 388, 388)) / (patch_size[0] * patch_size[1]) if pred_raw > threshold: pred_building_binary.append(1) else: pred_building_binary.append(0) np.save(os.path.join(task_dir, '1iter_pred_building_binary_{}.npy'.format(city_num)), pred_building_binary)
def readFromDiskIteratorTrain(self, image_dir, chipFiles, batch_size, patch_size, padding=(0, 0), dataAug=''): # this is a iterator for training if self.batch_code == 0: # pure random idx = np.random.permutation(len(chipFiles)) nDims = len(chipFiles[0]) while True: image_batch = np.zeros( (batch_size, patch_size[0], patch_size[1], nDims)) for cnt, randInd in enumerate(idx): row = chipFiles[randInd] blockList = [] nDims = 0 for file in row: img = util_functions.uabUtilAllTypeLoad( os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList).astype(np.float32) if self.block_mean is not None: block -= self.block_mean if dataAug != '': augDat = uabUtilreader.doDataAug( block, nDims, dataAug, is_np=True, img_mean=self.block_mean) else: augDat = block if (np.array(padding) > 0).any(): augDat = uabUtilreader.pad_block(augDat, padding) image_batch[cnt % batch_size, :, :, :] = augDat if ((cnt + 1) % batch_size == 0): yield image_batch[:, :, :, 1:], image_batch[:, :, :, :1] elif self.batch_code == 1: # random, batches from same tile tile_num, patch_per_tile, tile_name = get_tile_and_patch_num( chipFiles) group = group_by_tile_name(tile_name, chipFiles) tile_idx = np.random.permutation(tile_num) patch_idx = np.random.permutation(patch_per_tile) if patch_per_tile % batch_size != 0: comp_len = batch_size - patch_per_tile % batch_size patch_idx = np.append(patch_idx, patch_idx[:comp_len]) nDims = len(chipFiles[0]) while True: image_batch = np.zeros( (batch_size, patch_size[0], patch_size[1], nDims)) for randInd in tile_idx: for cnt, patchInd in enumerate(patch_idx): row = group[randInd][patchInd] blockList = [] nDims = 0 for file in row: img = util_functions.uabUtilAllTypeLoad( os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList).astype(np.float32) if self.block_mean is not None: block -= self.block_mean if dataAug != '': augDat = uabUtilreader.doDataAug( block, nDims, dataAug, is_np=True, img_mean=self.block_mean) else: augDat = block if (np.array(padding) > 0).any(): augDat = uabUtilreader.pad_block(augDat, padding) image_batch[cnt % batch_size, :, :, :] = augDat if ((cnt + 1) % batch_size == 0): yield image_batch[:, :, :, 1:], image_batch[:, :, :, :1] else: # random, batches has to from different tiles tile_num, patch_per_tile, tile_name = get_tile_and_patch_num( chipFiles) group = group_by_city_name(tile_name, chipFiles) tile_idx = np.random.permutation(len(group)) nDims = len(chipFiles[0]) while True: image_batch = np.zeros( (batch_size, patch_size[0], patch_size[1], nDims)) for cnt, randInd in enumerate(tile_idx): patchInd = np.random.randint(low=0, high=len(group[0])) row = group[randInd][patchInd] blockList = [] nDims = 0 for file in row: img = util_functions.uabUtilAllTypeLoad( os.path.join(image_dir, file)) if len(img.shape) == 2: img = np.expand_dims(img, axis=2) nDims += img.shape[2] blockList.append(img) block = np.dstack(blockList).astype(np.float32) if self.block_mean is not None: block -= self.block_mean if dataAug != '': augDat = uabUtilreader.doDataAug( block, nDims, dataAug, is_np=True, img_mean=self.block_mean) else: augDat = block if (np.array(padding) > 0).any(): augDat = uabUtilreader.pad_block(augDat, padding) image_batch[cnt % batch_size, :, :, :] = augDat if ((cnt + 1) % batch_size == 0): yield image_batch[:, :, :, 1:], image_batch[:, :, :, :1]