def _crf_with_alpha(ori_img,cam_dict, alpha): v = np.array(list(cam_dict.values())) bg_score = np.power(1 - np.max(v, axis=0, keepdims=True), alpha) bgcam_score = np.concatenate((bg_score, v), axis=0) crf_score = imutils.crf_inference(ori_img, bgcam_score, labels=bgcam_score.shape[0]) n_crf_al = np.zeros([21, bg_score.shape[1], bg_score.shape[2]]) n_crf_al[0, :, :] = crf_score[0, :, :] for i, key in enumerate(cam_dict.keys()): n_crf_al[key + 1] = crf_score[i + 1] return n_crf_al
def _crf_with_alpha(cam_dict, alpha): v = np.array(list(cam_dict.values())) bg_score = np.power(1 - np.max(v, axis=0, keepdims=True), alpha) bgcam_score = np.concatenate((bg_score, v), axis=0) print("v.shape ", v.shape) print("orig_img.shape ", orig_img.shape) print("type(bgcam_score)", type(bgcam_score)) print("type(orig_img) ", type(orig_img)) crf_score = imutils.crf_inference(orig_img, bgcam_score, labels=bgcam_score.shape[0]) print("after CRF......") input("[130].......") n_crf_al = dict() n_crf_al[0] = crf_score[0] for i, key in enumerate(cam_dict.keys()): n_crf_al[key + 1] = crf_score[i + 1] return n_crf_al
def rw_crf(predicted_dict, name=None): """ - orig_img: [3,H,W], np array - predicted_dict: dictionary, each item is a [H,W] predicted score for coresponding class """ v = np.array(list(predicted_dict.values())) pred_softmax = torch.nn.Softmax(dim=0) probs = pred_softmax(torch.tensor(v)).numpy() # orig_img = orig_img.data.cpu().numpy().transpose((1, 2, 0)) # [H,W,3] img_path = voc12.data.get_img_path(name, args.voc12_root) orig_img = np.asarray(Image.open(img_path)) # === note that orig_img have the shape [H,W,3] crf_score = imutils.crf_inference(orig_img, probs, labels=v.shape[0]) h, w = orig_img.shape[:2] crf_score_np = np.zeros(shape=(args.num_class, h, w)) crf_dict = dict() for i, key in enumerate(predicted_dict.keys()): crf_score_np[key] = crf_score[i] crf_dict[key] = crf_score[i] return crf_score_np, crf_dict
cam_full_arr = torch.from_numpy(cam_full_arr) cam_full_arr = F.avg_pool2d(cam_full_arr, 8, 8) cam_vec = cam_full_arr.view(21, -1) cam_rw = torch.matmul(cam_vec.cuda(), trans_mat) cam_rw = cam_rw.view(1, 21, dheight, dwidth) cam_rw = torch.nn.Upsample((img.shape[2], img.shape[3]), mode='bilinear')(cam_rw) cam_rw = cam_rw[:, :, :orig_shape[2], :orig_shape[3]] cam_rw = cam_rw.squeeze().data.cpu().numpy() aff_dict = {} aff_dict[0] = cam_rw[0] for i in range(20): if label[i] > 1e-5: aff_dict[i + 1] = cam_rw[i + 1] np.save(os.path.join(save_dir_aff, name + '.npy'), aff_dict) mask = np.argmax(cam_rw, axis=0) mask2png(mask, os.path.join(save_dir_aff, name + '.png')) v = np.array(list(aff_dict.values())) aff_crf = imutils.crf_inference(orig_img, v, labels=v.shape[0]) aff_crf_full_arr = np.zeros((21, orig_shape[2], orig_shape[3]), np.float32) cnt = 0 for k, v in aff_dict.items(): aff_crf_full_arr[k] = aff_crf[cnt] cnt += 1 np.save(os.path.join(save_dir_aff_crf, name + '.npy'), aff_crf) mask = np.argmax(aff_crf_full_arr, axis=0) mask2png(mask, os.path.join(save_dir_aff_crf, name + '.png'))
cam_vec = cam_full_arr.view(21, -1) cam_rw = torch.matmul(cam_vec.cuda(), trans_mat) cam_rw = cam_rw.view(1, 21, dheight, dwidth) cam_rw = torch.nn.Upsample((img.shape[2], img.shape[3]), mode='bilinear')(cam_rw) if args.crf: img_8 = img[0].numpy().transpose((1, 2, 0)) img_8 = np.ascontiguousarray(img_8) mean = (0.485, 0.456, 0.406) std = (0.229, 0.224, 0.225) img_8[:, :, 0] = (img_8[:, :, 0] * std[0] + mean[0]) * 255 img_8[:, :, 1] = (img_8[:, :, 1] * std[1] + mean[1]) * 255 img_8[:, :, 2] = (img_8[:, :, 2] * std[2] + mean[2]) * 255 img_8[img_8 > 255] = 255 img_8[img_8 < 0] = 0 img_8 = img_8.astype(np.uint8) cam_rw = cam_rw[0].cpu().numpy() cam_rw = imutils.crf_inference(img_8, cam_rw, t=1) cam_rw = torch.from_numpy(cam_rw).view(1, 21, img.shape[2], img.shape[3]).cuda() _, cam_rw_pred = torch.max(cam_rw, 1) res = np.uint8( cam_rw_pred.cpu().data[0])[:orig_shape[2], :orig_shape[3]] scipy.misc.imsave(os.path.join(args.out_rw, name + '.png'), res)