def visualize(cfg, vis_file_list=None, use_gpu=False, vis_dir="show", ckpt_dir=None, log_writer=None, local_test=False, **kwargs): for i in range(20): # Generator full colormap for maximum 256 classes color_map = get_color_map_list(256) res_map = np.ones((100, 100)) * i pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='L') pred_mask.putpalette(color_map) # pred_mask.save(vis_fn) pred_mask_np = np.array(pred_mask.convert("RGB")) im_pred = PILImage.fromarray(pred_mask_np) im_pred.save("/home/zxl/" + str(i) + ".png")
def visualize(cfg, vis_file_list=None, use_gpu=False, vis_dir="visual", ckpt_dir=None, log_writer=None, local_test=False, **kwargs): if vis_file_list is None: vis_file_list = cfg.DATASET.VIS_FILE_LIST dataset = SegDataset(file_list=vis_file_list, mode=ModelPhase.VISUAL, data_dir=cfg.DATASET.DATA_DIR) startup_prog = fluid.Program() test_prog = fluid.Program() pred, logit, out = build_model(test_prog, startup_prog, phase=ModelPhase.VISUAL) # Clone forward graph test_prog = test_prog.clone(for_test=True) # Generator full colormap for maximum 256 classes color_map = get_color_map_list(256) # Get device environment place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) exe.run(startup_prog) ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir if ckpt_dir is not None: print('load test model:', ckpt_dir) try: fluid.load(test_prog, os.path.join(ckpt_dir, 'model'), exe) except: fluid.io.load_params(exe, ckpt_dir, main_program=test_prog) save_dir = vis_dir makedirs(save_dir) fetch_list = [pred.name] test_reader = dataset.batch(dataset.generator, batch_size=1, is_test=True) img_cnt = 0 for imgs, grts, img_names, valid_shapes, org_shapes in test_reader: pred_shape = (imgs.shape[2], imgs.shape[3]) pred, = exe.run(program=test_prog, feed={'image': imgs}, fetch_list=fetch_list, return_numpy=True) num_imgs = pred.shape[0] # TODO: use multi-thread to write images for i in range(num_imgs): # Add more comments res_map = np.squeeze(pred[i, :, :, :]).astype(np.uint8) img_name = img_names[i] res_shape = (res_map.shape[0], res_map.shape[1]) if res_shape[0] != pred_shape[0] or res_shape[1] != pred_shape[1]: res_map = cv2.resize(res_map, pred_shape, interpolation=cv2.INTER_NEAREST) valid_shape = (valid_shapes[i, 0], valid_shapes[i, 1]) res_map = res_map[0:valid_shape[0], 0:valid_shape[1]] org_shape = (org_shapes[i, 0], org_shapes[i, 1]) res_map = cv2.resize(res_map, (org_shape[1], org_shape[0]), interpolation=cv2.INTER_NEAREST) png_fn = to_png_fn(img_name) # colorful segment result visualization vis_fn = os.path.join(save_dir, png_fn) dirname = os.path.dirname(vis_fn) makedirs(dirname) pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='L') #pred_mask.putpalette(color_map) pred_mask.save(vis_fn) img_cnt += 1 print("#{} visualize image path: {}".format(img_cnt, vis_fn)) # Use VisualDL to visualize image if log_writer is not None: # Calulate epoch from ckpt_dir folder name epoch = int(os.path.split(ckpt_dir)[-1]) print("VisualDL visualization epoch", epoch) pred_mask_np = np.array(pred_mask.convert("RGB")) log_writer.add_image("Predict/{}".format(img_name), pred_mask_np, epoch) # Original image # BGR->RGB img = cv2.imread(os.path.join(cfg.DATASET.DATA_DIR, img_name))[..., ::-1] log_writer.add_image("Images/{}".format(img_name), img, epoch) # add ground truth (label) images grt = grts[i] if grt is not None: grt = grt[0:valid_shape[0], 0:valid_shape[1]] grt_pil = PILImage.fromarray(grt.astype(np.uint8), mode='P') grt_pil.putpalette(color_map) grt_pil = grt_pil.resize((org_shape[1], org_shape[0])) grt = np.array(grt_pil.convert("256")) log_writer.add_image("Label/{}".format(img_name), grt, epoch) # If in local_test mode, only visualize 5 images just for testing # procedure if local_test and img_cnt >= 5: break
def visualize(cfg, vis_file_list=None, use_gpu=False, vis_dir="show", ckpt_dir=None, log_writer=None, local_test=False, **kwargs): if vis_file_list is None: vis_file_list = cfg.DATASET.VIS_FILE_LIST dataset = SegDataset(file_list=vis_file_list, mode=ModelPhase.VISUAL, data_dir=cfg.DATASET.DATA_DIR) startup_prog = fluid.Program() test_prog = fluid.Program() pred, logit, out = build_model(test_prog, startup_prog, phase=ModelPhase.VISUAL) # Clone forward graph test_prog = test_prog.clone(for_test=True) # Generator full colormap for maximum 256 classes color_map = get_color_map_list(256) # Get device environment place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) exe.run(startup_prog) ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir if ckpt_dir is not None: print('load test model:', ckpt_dir) try: fluid.load(test_prog, os.path.join(ckpt_dir, 'model'), exe) except: fluid.io.load_params(exe, ckpt_dir, main_program=test_prog) save_dir = "show" makedirs(save_dir) fetch_list = [pred.name, logit.name] test_reader = dataset.batch(dataset.generator, batch_size=1, is_test=True) img_cnt = 0 for imgs, grts, img_names, valid_shapes, org_shapes in test_reader: pred_shape = (imgs.shape[2], imgs.shape[3]) pred, logit = exe.run(program=test_prog, feed={'image': imgs}, fetch_list=fetch_list, return_numpy=True) num_imgs = pred.shape[0] # TODO: use multi-thread to write images for i in range(num_imgs): # Add more comments res_map = np.squeeze(pred[i, :, :, :]).astype(np.uint8) img_name = img_names[i] res_shape = (res_map.shape[0], res_map.shape[1]) if res_shape[0] != pred_shape[0] or res_shape[1] != pred_shape[1]: res_map = cv2.resize(res_map, pred_shape, interpolation=cv2.INTER_NEAREST) valid_shape = (valid_shapes[i, 0], valid_shapes[i, 1]) res_map = res_map[0:valid_shape[0], 0:valid_shape[1]] org_shape = (org_shapes[i, 0], org_shapes[i, 1]) res_map = cv2.resize(res_map, (org_shape[1], org_shape[0]), interpolation=cv2.INTER_NEAREST) png_fn = to_png_fn(img_name) # colorful segment result visualization vis_fn = os.path.join(save_dir, png_fn) dirname = os.path.dirname(vis_fn) makedirs(dirname) pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='L') pred_mask.putpalette(color_map) # pred_mask.save(vis_fn) pred_mask_np = np.array(pred_mask.convert("RGB")) im_pred = PILImage.fromarray(pred_mask_np) # Original image # BGR->RGB img = cv2.imread(os.path.join(cfg.DATASET.DATA_DIR, img_name))[..., ::-1] im_ori = PILImage.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # log_writer.add_image("Images/{}".format(img_name), img, epoch) # add ground truth (label) images im_pred_cat = PILImage.blend(im_ori, im_pred, 0.5) im_ori = join(im_ori, im_ori, flag="vertical") im_pred_cat = join(im_pred_cat, im_pred, flag="vertical") new_img = join(im_ori, im_pred_cat) new_img.save(vis_fn) img_cnt += 1 print("#{} show image path: {}".format(img_cnt, vis_fn))
def visualize(cfg, vis_file_list=None, use_gpu=False, vis_dir="visual", ckpt_dir=None, log_writer=None, local_test=False, **kwargs): if vis_file_list is None: vis_file_list = cfg.DATASET.TEST_FILE_LIST dataset = SegDataset(file_list=vis_file_list, mode=ModelPhase.VISUAL, data_dir=cfg.DATASET.DATA_DIR) startup_prog = fluid.Program() test_prog = fluid.Program() pred, logit = build_model(test_prog, startup_prog, phase=ModelPhase.VISUAL) # Clone forward graph test_prog = test_prog.clone(for_test=True) # Generator full colormap for maximum 256 classes color_map = get_color_map_list(cfg.DATASET.NUM_CLASSES**2 if cfg.DATASET.NUM_CLASSES**2 < 256 else 256) # Get device environment place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) exe.run(startup_prog) ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir if ckpt_dir is not None: print('load test model:', ckpt_dir) try: fluid.load(test_prog, os.path.join(ckpt_dir, 'model'), exe) except: fluid.io.load_params(exe, ckpt_dir, main_program=test_prog) save_dir = vis_dir makedirs(save_dir) fetch_list = [pred.name] test_reader = dataset.batch(dataset.generator, batch_size=1, is_test=True) img_cnt = 0 def exe_run(): if cfg.DATASET.INPUT_IMAGE_NUM == 1: for imgs, grts, img_names, valid_shapes, org_shapes in test_reader: pred_shape = (imgs.shape[2], imgs.shape[3]) pred, = exe.run(program=test_prog, feed={'image1': imgs}, fetch_list=fetch_list, return_numpy=True) yield pred, pred_shape, grts, img_names, valid_shapes, org_shapes else: for img1s, img2s, grts, img1_names, img2_names, valid_shapes, org_shapes in test_reader: pred_shape = (img1s.shape[2], img1s.shape[3]) pred, = exe.run(program=test_prog, feed={ 'image1': img1s, 'image2': img2s }, fetch_list=fetch_list, return_numpy=True) yield pred, pred_shape, grts, img1_names, valid_shapes, org_shapes for pred, pred_shape, grts, img_names, valid_shapes, org_shapes in exe_run( ): idx = pred.shape[0] if cfg.DATASET.INPUT_IMAGE_NUM == 2 and cfg.VIS.SEG_FOR_CD: idx = pred.shape[0] // cfg.DATASET.INPUT_IMAGE_NUM pred1, pred2 = pred[:idx], pred[ idx:] # fluid.layers.split(pred, 2, dim=0) num_imgs = pred1.shape[0] # TODO: use multi-thread to write images for i in range(num_imgs): # Add more comments res_map_list = [] for pred in [pred1, pred2]: if pred.shape[0] == 0: continue #res_map = np.squeeze(pred[i, :, :, :]).astype(np.uint8) res_map = np.squeeze(pred[i, :, :, :]).astype(np.float32) res_shape = (res_map.shape[0], res_map.shape[1]) if res_shape[0] != pred_shape[0] or res_shape[1] != pred_shape[ 1]: res_map = cv2.resize(res_map, pred_shape, interpolation=cv2.INTER_NEAREST) valid_shape = (valid_shapes[i, 0], valid_shapes[i, 1]) res_map = res_map[0:valid_shape[0], 0:valid_shape[1]] org_shape = (org_shapes[i, 0], org_shapes[i, 1]) res_map = cv2.resize(res_map, (org_shape[1], org_shape[0]), interpolation=cv2.INTER_NEAREST) res_map_list.append(res_map) img_name = img_names[i] png_fn = to_png_fn(img_name) # colorful segment result visualization vis_fn = os.path.join(save_dir, png_fn) dirname = os.path.dirname(vis_fn) makedirs(dirname) if cfg.DATASET.INPUT_IMAGE_NUM == 1 or \ (cfg.DATASET.INPUT_IMAGE_NUM == 2 and not cfg.VIS.SEG_FOR_CD): res_map = res_map_list[0] if cfg.VIS.RAW_PRED: #pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='L') #pred_mask.save(vis_fn) np.save(vis_fn.replace(".png", ".npy"), res_map.astype(np.float32)) else: if cfg.VIS.ADD_LABEL: grt_im = cv2.resize(grts[i], pred_shape, interpolation=cv2.INTER_NEAREST) res_map = np.hstack((res_map, grt_im)) pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='P') pred_mask.putpalette(color_map) pred_mask.save(vis_fn) else: res_map1, res_map2 = res_map_list diff = res_map1 * cfg.DATASET.NUM_CLASSES + res_map2 unchange_idx = np.where((res_map1 - res_map2) == 0) diff[unchange_idx] = 0 res_map = np.hstack((res_map1, res_map2, diff)) pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='P') pred_mask.putpalette(color_map) pred_mask.save(vis_fn) img_cnt += 1 print("#{} visualize image path: {}".format(img_cnt, vis_fn)) # Use VisualDL to visualize image if log_writer is not None: # Calulate epoch from ckpt_dir folder name epoch = int(os.path.split(ckpt_dir)[-1]) print("VisualDL visualization epoch", epoch) pred_mask_np = np.array(pred_mask.convert("RGB")) log_writer.add_image("Predict/{}".format(img_name), pred_mask_np, epoch) # Original image # BGR->RGB img = cv2.imread(os.path.join(cfg.DATASET.DATA_DIR, img_name))[..., ::-1] log_writer.add_image("Images/{}".format(img_name), img, epoch) # add ground truth (label) images grt = grts[i] if grt is not None: grt = grt[0:valid_shape[0], 0:valid_shape[1]] grt_pil = PILImage.fromarray(grt.astype(np.uint8), mode='P') grt_pil.putpalette(color_map) grt_pil = grt_pil.resize((org_shape[1], org_shape[0])) grt = np.array(grt_pil.convert("RGB")) log_writer.add_image("Label/{}".format(img_name), grt, epoch) # If in local_test mode, only visualize 5 images just for testing # procedure if local_test and img_cnt >= 5: break