def predict_mask(image_file, smooth, labels): im = pylab.imread(image_file) net.blobs['images'].data[0] = preprocess(im, 321) net.forward() scores = np.transpose(net.blobs['fc8-SEC'].data[0], [1, 2, 0]) d1, d2 = float(im.shape[0]), float(im.shape[1]) scores_exp = np.exp(scores - np.max(scores, axis=2, keepdims=True)) probs = scores_exp / np.sum(scores_exp, axis=2, keepdims=True) probs = nd.zoom(probs, (d1 / probs.shape[0], d2 / probs.shape[1], 1.0), order=1) eps = 0.00001 probs[probs < eps] = eps if smooth: probs = krahenbuhl2013.CRF(im, np.log(probs), scale_factor=1.0) labels = labels.tolist() labels.insert(0, 0) probs_selected = probs[:, :, labels] probs_c = np.argmax(probs_selected, axis=2) result = np.vectorize(lambda x: labels[x])(probs_c) prob_max = np.max(probs, axis=2) # result[prob_max < 0.85] = 255 return result
def predict_mask(image_file, smooth=True): im = pylab.imread(image_file) net.blobs['images'].data[0] = preprocess(im, 321) net.forward() scores = np.transpose(net.blobs['fc8-SEC'].data[0], [1, 2, 0]) d1, d2 = float(im.shape[0]), float(im.shape[1]) scores_exp = np.exp(scores - np.max(scores, axis=2, keepdims=True)) probs = scores_exp / np.sum(scores_exp, axis=2, keepdims=True) probs = nd.zoom(probs, (d1 / probs.shape[0], d2 / probs.shape[1], 1.0), order=1) img_id = os.path.splitext(os.path.basename(image_file))[0] np.save(img_id, probs) eps = 0.00001 probs[probs < eps] = eps if smooth: result = np.argmax(krahenbuhl2013.CRF(im, np.log(probs), scale_factor=1.0), axis=2) else: result = np.argmax(probs, axis=2) return result
def predict_mask(image_file, smooth=True): im = pylab.imread(image_file) d1, d2 = float(im.shape[0]), float(im.shape[1]) scores_all = 0 for size in [241, 321, 401]: im_process = preprocess(im, size) net.blobs['images'].reshape(*im_process.shape) net.blobs['images'].data[...] = im_process net.forward() scores = np.transpose(net.blobs['fc8-SEC'].data[0], [1, 2, 0]) scores = nd.zoom(scores, (d1 / scores.shape[0], d2 / scores.shape[1], 1.0), order=1) scores_all += scores scores_exp = np.exp(scores_all - np.max(scores_all, axis=2, keepdims=True)) probs = scores_exp / np.sum(scores_exp, axis=2, keepdims=True) eps = 0.00001 probs[probs < eps] = eps if smooth: result = np.argmax(krahenbuhl2013.CRF(im, np.log(probs), scale_factor=1.0), axis=2) # result = np.argmax(dense_crf(probs, im), axis=2) else: result = np.argmax(probs, axis=2) return result
def multi_iter_CRF(self, mask_res, img): U = np.log(np.transpose(mask_res,(1,2,0))) img_uint8 = img.astype('uint8') i_map = 0 for idx, val in enumerate(self.iters): self.map[idx,:,:] = np.argmax(krahenbuhl2013.CRF(img_uint8, U, maxiter=val, scale_factor=1.0), axis=2) if self.iters[0] == 0: raw_map = self.map[0,:,:]
def multi_iter_CRF(self, mask_res, img): U = np.log(np.transpose(mask_res, (1, 2, 0))) img_uint8 = img.astype('uint8') map = np.zeros([self.num_maps, img.shape[0], img.shape[1]]) for idx, val in enumerate(self.iters): map[idx, :, :] = np.argmax(krahenbuhl2013.CRF(img_uint8, U, maxiter=val, scale_factor=1.0), axis=2) return map
def predict_mask(image_file, net, smooth=True, multi_scale=True): im = pylab.imread(image_file) d1, d2 = float(im.shape[0]), float(im.shape[1]) scores_all = 0 scales = [321] if multi_scale is False else [ int(0.75 * 385), 385, int(1.25 * 385) ] for im_size in scales: im_process = preprocess(im, im_size) net.blobs['images'].reshape(*im_process.shape) net.blobs['images'].data[...] = im_process net.forward() scores = np.transpose(net.blobs[options.out_blob].data[0], [1, 2, 0]) scores = nd.zoom(scores, (d1 / scores.shape[0], d2 / scores.shape[1], 1.0), order=1) # set_trace() # im_flip = im_process[:, :, :, ::-1] # net.blobs['images'].reshape(*im_flip.shape) # net.blobs['images'].data[...] = im_flip # net.forward() # score_flip = np.transpose(net.blobs[options.out_blob].data[0], [1, 2, 0]) # score_flip = nd.zoom(score_flip, (d1 / score_flip.shape[0], d2 / score_flip.shape[1], 1.0), order=1) # score_flip = score_flip[:, ::-1, :] scores_all += scores # scores_all += score_flip scores_exp = np.exp(scores_all - np.max(scores_all, axis=2, keepdims=True)) probs = scores_exp / np.sum(scores_exp, axis=2, keepdims=True) eps = 0.00001 probs[probs < eps] = eps if smooth: result = krahenbuhl2013.CRF(im, np.log(probs), scale_factor=1.0) else: result = probs return result
def CRF(im, score, scale=12.0): """ perform CRF process Parameter --------- im: [H, W, 3] score: [h, w, c] scale: scale factor of CRF """ im_h, im_w = im.shape[:2] h, w = score.shape[:2] if im_h != h: im = nd.zoom(im, (h / im_h, w / im_w, 1.0), order=1) prob = softmax(score) return krahenbuhl2013.CRF(im, prob, scale_factor=scale)
def predict_color_mask(image_file, model, smooth, gpu_id, cls_label): cls_label = np.insert(cls_label, 0, 1) cls_label = np.squeeze(np.asarray(np.nonzero(cls_label), dtype=int)) im = pylab.imread(image_file) d1, d2 = float(im.shape[0]), float(im.shape[1]) image = torch.from_numpy(preprocess(im, 321, IMAGE_MEAN_VALUE).astype(np.float32)) image = image.cuda(gpu_id) featmap = model(image) featmap = featmap.reshape(21, 41, 41).detach().cpu().numpy().transpose(1, 2, 0) featmap = nd.zoom(featmap, (d1 / featmap.shape[0], d2 / featmap.shape[1], 1.0), order=2) if smooth: crf_pred = krahenbuhl2013.CRF(im, np.array(featmap), scale_factor=1.0) else: crf_pred = featmap output = label2rgb(np.argmax(featmap,axis=2), cls_label) pred = label2rgb(np.argmax(crf_pred,axis=2), cls_label) return output, pred
def predict_mask(image_file, model, smooth, gpu_id): im = pylab.imread(image_file) image = torch.from_numpy(preprocess(im, 321, IMAGE_MEAN_VALUE).astype(np.float32)) image = image.cuda(gpu_id) output = model(image) scores = output.reshape(21, 41, 41).detach().cpu().numpy().transpose(1, 2, 0) d1, d2 = float(im.shape[0]), float(im.shape[1]) scores_exp = np.exp(scores - np.max(scores, axis=2, keepdims=True)) probs = scores_exp / np.sum(scores_exp, axis=2, keepdims=True) probs = nd.zoom(probs, (d1 / probs.shape[0], d2 / probs.shape[1], 1.0), order=1) eps = 0.00001 # eps = 0.00001 * 50. probs[probs < eps] = eps if smooth: result = np.argmax(krahenbuhl2013.CRF(im, np.log(probs), scale_factor=1.0), axis=2) else: result = np.argmax(probs, axis=2) return result
def crf(self, images, probs): np_ims = np.transpose(images.data.cpu().numpy(), [0, 2, 3, 1]) # (n, H, W, c) np_probs = np.transpose(probs.data.cpu().numpy(), [0, 2, 3, 1]) # (n, h, w, c) im_h, im_w = np_ims.shape[1:3] h, w = np_probs.shape[1:3] np_ims = nd.zoom(np_ims, (1.0, h / im_h, w / im_w, 1.0), order=1) batch_size = np_ims.shape[0] list_crf = [] for k in range(batch_size): list_crf.append( krahenbuhl2013.CRF(np_ims[k], np_probs[k], scale_factor=12.0)) crf_prob = np.stack(list_crf, axis=0) crf_prob = np.transpose(crf_prob, [0, 3, 1, 2]) crf_prob[crf_prob < self.eps] = self.eps crf_prob = crf_prob / np.sum(crf_prob, axis=1, keepdims=True) result = torch.from_numpy(crf_prob).float().cuda(probs.get_device()) return result
def crf_single(inp): im, prob = inp[0], inp[1] im = np.squeeze(im, axis=0) prob = np.squeeze(prob, axis=0) return krahenbuhl2013.CRF(im, prob, scale_factor=12.0)