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
# is applied in order to keep only those that are different bboxes_in_img = ut.non_max_suppression(bboxes_in_img, NON_MAX_SUP_TH) # If sliding window with convolution flag is set if F_CONV: # Iterate over the different masks previously calculated. # For each max, compute the bounding boxes found in the mask for mask in masks: pass # As the bounding box can be found in different masks, non maximal supression # is applied in order to keep only those that are different bboxes_in_img = ut.non_max_suppression(bboxes_in_img, NON_MAX_SUP_TH) # Final mask: Merge of the previous masks mask = ut.merge_masks(masks) # Get mask name from raw name fname = ut.raw2mask(raw_name) # Save mask in directory ut.save_image(mask, METHOD_DIR, fname) logger.info('{fname} mask saved in {directory}'.format(fname=fname, directory=METHOD_DIR)) # If bounding boxes were found in the image, save in the dictionary bboxes_found if len(bboxes_in_img) != 0: l = bboxes_found.get(raw_name, []) if type(l).__module__ == np.__name__: l = l.tolist() l.extend(bboxes_in_img) bboxes_found[raw_name] = ut.non_max_suppression(l, NON_MAX_SUP_TH)
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
def predict_img_batch(net, imgpath, lblpath, batchsize=10, scale_factor=0.5, out_threshold=0.5, use_dense_crf=True, use_gpu=True): """return fullmask with size (C, H, W)""" net.eval() print('imgpath', imgpath ) predictdataset = Dataset_predict(imgpath, "L", 0.5) predictdatagen = data.DataLoader(predictdataset,batchsize) for iB, (img, imgids) in enumerate(predictdatagen): print('batch num {}'.format(iB)) # print('img.shape', img.shape ) """ img with size (N H W C)""" # print('np.max(img))', np.max(np.array(img))) # img = normalize(img) # print('np.max(img))', np.max(np.array(img))) left_square, right_square = split_img_into_squares_batch(img) img_width = int(img.shape[2]/scale_factor) "(N H W C) ---> (N C H W)" # print('left_square.shape', left_square.shape ) left_square = np.transpose(left_square, [0, 3, 1, 2]) # (N C H W) right_square = np.transpose(right_square, [0, 3, 1, 2]) # print('type(left_square)', type(left_square) ) # X_left = torch.from_numpy(left_square) # X_right = torch.from_numpy(right_square) X_left = left_square.type(torch.FloatTensor) X_right = right_square.type(torch.FloatTensor) if use_gpu: X_left = X_left.cuda() X_right = X_right.cuda() with torch.no_grad(): output_left = net(X_left) # (N C H W) output_right = net(X_right) print('output_left.shape', output_left.shape) print('output_right.shape', output_right.shape) left_probs = output_left # (N C H W) right_probs = output_right left_mask_np = left_probs.cpu().numpy() # (N C H W) right_mask_np = right_probs.cpu().numpy() # (N C H W) for iN, imgid in enumerate(imgids): left_mask_np_n = left_mask_np[iN] # ( C H W) right_mask_np_n = right_mask_np[iN] left_mask_np_n = np.transpose(left_mask_np_n, axes=[1, 2, 0]) # (H W C) right_mask_np_n = np.transpose(right_mask_np_n, axes=[1, 2, 0]) if not scale_factor == 1: right_mask_np_n = resize_np(right_mask_np_n, 1/scale_factor) # (H/2 W/2 C) left_mask_np_n = resize_np(left_mask_np_n, 1/scale_factor) right_mask_np_n = np.transpose(right_mask_np_n, axes=[2,0,1]) # (C H W) left_mask_np_n = np.transpose(left_mask_np_n, axes=[2,0,1]) print('left_mask_np.shape_n', left_mask_np_n.shape ) print('img_width', img_width ) full_mask = merge_masks(left_mask_np_n, right_mask_np_n, img_width) full_mask = np.transpose(full_mask, axes=[1,2,0]) # (H W C) print('full_mask.shape', full_mask.shape ) np.savez_compressed(lblpath + '/' + imgid.split('.png')[0] + '_mask.npz', label=full_mask) ## default name is arr_0