def predict_img(net, full_img, scale_factor=0.5, out_threshold=0.5, use_dense_crf=True, use_gpu=False): img_height = full_img.size[1] img_width = full_img.size[0] img = resize_and_crop(full_img, scale=scale_factor) img = normalize(img) left_square, right_square = split_img_into_squares(img) left_square = hwc_to_chw(left_square) right_square = hwc_to_chw(right_square) X_left = torch.from_numpy(left_square).unsqueeze(0) X_right = torch.from_numpy(right_square).unsqueeze(0) if use_gpu: X_left = X_left.cuda() X_right = X_right.cuda() with torch.no_grad(): output_left = net(X_left) output_right = net(X_right) left_probs = F.sigmoid(output_left).squeeze(0) right_probs = F.sigmoid(output_right).squeeze(0) ''' tf = transforms.Compose( [ transforms.ToPILImage(), transforms.Resize(img_height), transforms.ToTensor() ] ) ''' tf = transforms.Compose([ transforms.ToPILImage(), transforms.Scale(img_height), transforms.ToTensor() ]) left_probs = tf(left_probs.cpu()) right_probs = tf(right_probs.cpu()) left_mask_np = left_probs.squeeze().cpu().numpy() right_mask_np = right_probs.squeeze().cpu().numpy() full_mask = merge_masks(left_mask_np, right_mask_np, img_width) if use_dense_crf: full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask) return full_mask > out_threshold
def predict_img_batch(net, full_img, scale_factor=0.5, out_threshold=0.5, use_dense_crf=True, use_gpu=True): """return fullmask with size (C, H, W)""" net.eval() img_height = full_img.size[1] img_width = full_img.size[0] print('imgheight', img_height) print('imgwidth', img_width) img = resize_and_crop(full_img, scale=scale_factor) img = normalize(img) if len(img.shape) == 2: img = img[..., np.newaxis] print('img.shape', img.shape) left_square, right_square = split_img_into_squares(img) left_square = hwc_to_chw(left_square) right_square = hwc_to_chw(right_square) X_left = torch.from_numpy(left_square).unsqueeze(0) X_right = torch.from_numpy(right_square).unsqueeze(0) if use_gpu: X_left = X_left.cuda() X_right = X_right.cuda() with torch.no_grad(): output_left = net(X_left) output_right = net(X_right) print('output_left.shape', output_left.shape) print('output_right.shape', output_right.shape) left_probs = output_left.squeeze(0) right_probs = output_right.squeeze(0) # # if not scale_factor==1: # tf = transforms.Compose( # [ # transforms.ToPILImage(), # transforms.Resize(img_height), # transforms.ToTensor() # ] # ) # # left_probs = tf(left_probs.cpu()) # right_probs = tf(right_probs.cpu()) # print('left_probs', left_probs.shape) # print('right_probs', right_probs.shape) left_mask_np = left_probs.squeeze().cpu().numpy() right_mask_np = right_probs.squeeze().cpu().numpy() left_mask_np = np.transpose(left_mask_np, axes=[1, 2, 0]) right_mask_np = np.transpose(right_mask_np, axes=[1, 2, 0]) if not scale_factor == 1: right_mask_np = resize_np(right_mask_np, 1 / scale_factor) left_mask_np = resize_np(left_mask_np, 1 / scale_factor) print('left_mask_np', left_mask_np.shape) print('right_mask_np', right_mask_np.shape) left_mask_np = np.transpose(left_mask_np, axes=[2, 0, 1]) right_mask_np = np.transpose(right_mask_np, axes=[2, 0, 1]) full_mask = merge_masks(left_mask_np, right_mask_np, img_width) # if use_dense_crf: if 0: full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask) return full_mask
def predict_img(net, full_img, out_threshold=0.5, use_dense_crf=True, use_gpu=False, channels=2): img_height = full_img.shape[0] img_width = full_img.shape[1] left_square, right_square = split_img_into_squares(img.copy() / 255) # left_square = hwc_to_chw(left_square) # right_square = hwc_to_chw(right_square) # # X_left = torch.from_numpy(left_square).unsqueeze(0) # X_right = torch.from_numpy(right_square).unsqueeze(0) normalize = T.Normalize(mean=[0.4, 0.4, 0.4], std=[0.4, 0.4, 0.4]) transforms4imgs = T.Compose([ T.ToTensor(), normalize ]) X_left = transforms4imgs(left_square) X_right = transforms4imgs(right_square) X_left = Variable(X_left).unsqueeze(0) X_right = Variable(X_right).unsqueeze(0) if use_gpu: X_left = X_left.cuda() X_right = X_right.cuda() net.eval() output_left = net(X_left) output_right = net(X_right) if channels > 1: left_probs = torch.argmax(output_left, dim=1).float() right_probs = torch.argmax(output_right, dim=1).float() else: left_probs = F.sigmoid(output_left).squeeze(0) right_probs = F.sigmoid(output_right).squeeze(0) tf = transforms.Compose( [ transforms.ToPILImage(), transforms.Resize(img_height), transforms.ToTensor() ] ) left_probs = tf(left_probs.cpu()) right_probs = tf(right_probs.cpu()) left_mask_np = left_probs.squeeze().cpu().numpy() right_mask_np = right_probs.squeeze().cpu().numpy() full_mask = merge_masks(left_mask_np, right_mask_np, img_width) # if use_dense_crf: # full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask) return full_mask > out_threshold
def predict_img(net, full_img, scale_factor=0.5, out_threshold=0.5, use_gpu=False): pst = time.perf_counter() net.eval() #print(' 0 running time: %s seconds ' %(( time.clock() -pst))) img_height = full_img.size[1] # print(img_height) img_width = full_img.size[0] # print(full_img.size) # 2048,1229 img = resize_and_crop(full_img, scale=scale_factor) #pdb.set_trace() #print(' 1 running time: %s seconds ' %(( time.clock() -pst))) img = normalize(img) #print(' 2 running time: %s seconds ' %(( time.clock() -pst))) # print(img.shape) # 614,1024,3 left_square, right_square = split_img_into_squares(img) # print(right_square.shape) # 614,614,3 left_square = hwc_to_chw(left_square) right_square = hwc_to_chw(right_square) #print(' 3 running time: %s seconds ' %(( time.clock() -pst))) X_left = torch.from_numpy(left_square).unsqueeze(0) X_right = torch.from_numpy(right_square).unsqueeze(0) #print(' 4 running time: %s seconds ' %(( time.clock() -pst))) #outstart = time.clock() if use_gpu: X_left = X_left.cuda() X_right = X_right.cuda() #print(' 5 running time: %s seconds ' %(( time.clock() -pst))) with torch.no_grad(): torch.cuda.synchronize() st = time.perf_counter() # print(X_left.shape) # 1,3,614,614 output_left = net(X_left) output_right = net(X_right) torch.cuda.synchronize() st1 = time.perf_counter() #outend = time.clock() print(' Unet++ --------------------> running time: %s seconds ' % (st1 - st)) left_probs = output_left.squeeze(0) right_probs = output_right.squeeze(0) # print(' squeeze running time: %s seconds ' %((time.perf_counter()-st1))) if (left_probs.shape[1] != img_height): tf = transforms.Compose([ transforms.ToPILImage(), transforms.Resize(img_height), transforms.ToTensor() ]) left_probs = tf(left_probs.cpu()) right_probs = tf(right_probs.cpu()) print("Transform done!") #print(' 8 running time: %s seconds ' %(( time.clock() -pst))) #lstart = time.clock() #left_probs.cpu() #print(' transforms running time: %s seconds ' %(( time.time() -pst))) st = time.perf_counter() left_mask_np = left_probs.squeeze().cpu().numpy() end1 = time.perf_counter() - st #print(left_probs.shape) #pdb.set_trace() # print(' tonumpy1 running time: %s seconds ' %(end1)) st = time.perf_counter() right_mask_np = right_probs.squeeze().cpu().numpy() end2 = time.perf_counter() - st # print(' tonumpy2 running time: %s seconds ' %(end2)) full_mask = merge_masks(left_mask_np, right_mask_np, img_width) # print(' 9 running time: %s seconds ' %(( time.perf_counter() -pst))) #pdb.set_trace() full_mask[full_mask >= out_threshold] = 1 full_mask[full_mask < out_threshold] = 0 #------------------------------------------------------------------------------- #newmask = dense_crf(np.array(full_img).astype(np.uint8), full_mask) #lend = time.clock() #print(' running time: %s seconds ' %((lend-pst))) #pdb.set_trace() return full_mask > out_threshold