| import torch |
| import random |
|
|
| def generate_click_prompt(img, msk, pt_label = 1): |
| |
| pt_list = [] |
| msk_list = [] |
| b, c, h, w, d = msk.size() |
| msk = msk[:,0,:,:,:] |
| for i in range(d): |
| pt_list_s = [] |
| msk_list_s = [] |
| for j in range(b): |
| msk_s = msk[j,:,:,i] |
| indices = torch.nonzero(msk_s) |
| if indices.size(0) == 0: |
| |
| random_index = torch.randint(0, h, (2,)).to(device = msk.device) |
| new_s = msk_s |
| else: |
| random_index = random.choice(indices) |
| label = msk_s[random_index[0], random_index[1]] |
| new_s = torch.zeros_like(msk_s) |
| |
| new_s = (msk_s == label).to(dtype = torch.float) |
| |
| pt_list_s.append(random_index) |
| msk_list_s.append(new_s) |
| pts = torch.stack(pt_list_s, dim=0) |
| msks = torch.stack(msk_list_s, dim=0) |
| pt_list.append(pts) |
| msk_list.append(msks) |
| pt = torch.stack(pt_list, dim=-1) |
| msk = torch.stack(msk_list, dim=-1) |
| msk = msk.unsqueeze(1) |
| return img, pt, msk |
|
|
| import torch |
|
|
| def get_click_prompt_eval(datapack, opt): |
| """ |
| 返回与 get_click_prompt 相同的结构:(coords_torch, labels_torch) |
| - coords_torch: float32, [B,P,2] |
| - labels_torch: torch.int, [B,P] |
| """ |
| device = opt.device |
|
|
| |
| if 'pt' not in datapack or 'p_label' not in datapack: |
| b, _, h, w = datapack['image'].shape |
| cx = (w - 1) / 2.0 |
| cy = (h - 1) / 2.0 |
| coords_torch = torch.tensor([[[cx, cy]]], dtype=torch.float32, device=device).repeat(b, 1, 1) |
| labels_torch = torch.zeros((b, 1), dtype=torch.int, device=device) |
| return (coords_torch, labels_torch) |
|
|
| |
| coords_torch = torch.as_tensor(datapack['pt'], dtype=torch.float32, device=device) |
| labels_torch = torch.as_tensor(datapack['p_label'], dtype=torch.int, device=device) |
|
|
| |
| if coords_torch.ndim == 2: |
| if 'image' in datapack and datapack['image'].shape[0] == coords_torch.shape[0] == coords_torch.shape[-2]: |
| |
| coords_torch = coords_torch.unsqueeze(1) |
| if labels_torch.ndim == 1 and labels_torch.shape[0] == datapack['image'].shape[0]: |
| labels_torch = labels_torch.unsqueeze(1) |
| else: |
| |
| coords_torch = coords_torch.unsqueeze(0) |
| if labels_torch.ndim == 1: |
| labels_torch = labels_torch.unsqueeze(0) |
|
|
| elif coords_torch.ndim == 3: |
| |
| pass |
| else: |
| raise ValueError(f"Unexpected pt shape: {coords_torch.shape}") |
|
|
| |
| if labels_torch.ndim == 1: |
| labels_torch = labels_torch.unsqueeze(0) |
|
|
| return (coords_torch, labels_torch) |
|
|
|
|
| def get_click_prompt(datapack, opt): |
| if 'pt' not in datapack: |
| imgs, pt, masks = generate_click_prompt(imgs, masks) |
| else: |
| pt = datapack['pt'] |
| point_labels = datapack['p_label'] |
|
|
| point_coords = pt |
| coords_torch = torch.as_tensor(point_coords, dtype=torch.float32, device=opt.device) |
| labels_torch = torch.as_tensor(point_labels, dtype=torch.int, device=opt.device) |
| if len(pt.shape) == 2: |
| coords_torch, labels_torch = coords_torch[None, :, :], labels_torch[None, :] |
| pt = (coords_torch, labels_torch) |
| return pt |
|
|