forked from aleemsidra/SaLIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sam_clip_3d.py
220 lines (158 loc) · 8.04 KB
/
sam_clip_3d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import cv2
import json
import random
import numpy as np
from torch import Tensor
# from utils.utils import log_images
import wandb
import clip
from IPython import embed
from segment_anything import SamAutomaticMaskGenerator
from sam_clip import retrieve_relevant_crop, get_crops
import torch
from typing import List
from PIL import Image
from torchvision import transforms
from torchvision.transforms.functional import to_pil_image
from datetime import datetime
from tqdm import tqdm
from segment_anything.utils.transforms import ResizeLongestSide
from utils.utils import log_images
def sam_mask_generator_params(sam, model, preprocess, resize_transform, dataset, config):
num_slices = 5
num_iterations = 5
points_per_side_range = [16, 32, 64]
pred_iou_thresh_range = [0.2, 0.5, 0.8, 0.9]
stability_score_thresh_range = [0.2, 0.5, 0.8, 0.95]
random_indices = random.sample(range(len(dataset)), 2)
print("Random Indices:", random_indices)
best_score = -1
best_params = None
for _ in tqdm(range(num_iterations), desc="Iteration: Hyper-params tuning", unit="iteration"):
points_per_side = random.choice(points_per_side_range)
stability_score_thresh = random.choice(stability_score_thresh_range)
pred_iou_thresh = random.choice(pred_iou_thresh_range)
print(f"hyper-params: points_per_side: {points_per_side}, stability_score_thresh: {stability_score_thresh},pred_iou_thresh: {pred_iou_thresh} ")
avg_dice_coeff = 0
for idx in random_indices:
print(f"image id: {idx}")
input_samples, gt_samples = dataset[idx]
spacing = input_samples.shape[0] // num_slices
slices_dice_coeff = []
for slice_idx in range(spacing, input_samples.shape[0], spacing):
img_slice = input_samples[slice_idx, :, :]
mask_generator = SamAutomaticMaskGenerator(
model=sam,
points_per_side=points_per_side,
points_per_batch=256,
pred_iou_thresh=pred_iou_thresh,
stability_score_thresh=stability_score_thresh,
crop_n_layers=1,
crop_n_points_downscale_factor=2,
min_mask_region_area=200,
)
bbox = get_bbox(img_slice, model, preprocess, mask_generator, config) # bbox via SAM+CLIP
# Format required by SAM predictor
batched_input = [{
'image': prepare_image(img_slice, resize_transform, "cuda"),
'boxes': resize_transform.apply_boxes_torch(torch.from_numpy(bbox), img_slice.shape[:2]).to("cuda"),
'original_size': img_slice[0,:,:].shape
}]
preds = sam(batched_input, multimask_output=False)
mask = torch.sigmoid(preds.squeeze()).cpu().detach().numpy() > 0.5
slices_dice_coeff.append(dice_coeff(torch.tensor(mask, dtype=torch.float32), gt_samples[slice_idx].squeeze()))
del preds
del mask
avg_dice_coeff += np.mean(slices_dice_coeff)
avg_dice_coeff /= len(random_indices)
print("avg_dice_coeff", avg_dice_coeff)
if avg_dice_coeff > best_score:
best_score = avg_dice_coeff
print(f"best dice: {best_score}")
best_params = {
'points_per_side': points_per_side,
'pred_iou_thresh': pred_iou_thresh,
'stability_score_thresh': stability_score_thresh,
}
print("Best Parameters:", best_params)
mask_generator = SamAutomaticMaskGenerator(
model=sam,
points_per_side= best_params["points_per_side"],
points_per_batch= 128,
pred_iou_thresh= best_params["pred_iou_thresh"],
stability_score_thresh= best_params["stability_score_thresh"],
crop_n_layers=1,
crop_n_points_downscale_factor=2,
min_mask_region_area=200, # Requires open-cv to run post-processing
)
return mask_generator
def dice_coeff(input: Tensor, target: Tensor, reduce_batch_first: bool = False, epsilon: float = 1e-6):
assert input.size() == target.size()
assert input.dim() == 3 or not reduce_batch_first
sum_dim = (-1, -2) if input.dim() == 2 or not reduce_batch_first else (-1, -2, -3)
inter = 2 * (input * target).sum(dim=sum_dim)
sets_sum = input.sum(dim=sum_dim) + target.sum(dim=sum_dim)
sets_sum = torch.where(sets_sum == 0, inter, sets_sum)
dice = (inter + epsilon) / (sets_sum + epsilon)
return dice.mean()
def prepare_image(image, transform, device):
bgr_img = cv2.cvtColor( image[0,:,:], cv2.COLOR_GRAY2BGR)
scaled_tensor = 255 * (bgr_img - bgr_img.min()) / (bgr_img.max() - bgr_img.min())
image = scaled_tensor.astype(np.uint8)
image = transform.apply_image(image)
image = torch.as_tensor(image)
image = image.to(device)
return image.permute(2, 0, 1).contiguous()
def get_bbox(image, model, preprocess, mask_generator, config):
rgb_slice = cv2.cvtColor(image.squeeze(), cv2.COLOR_GRAY2BGR)
image = cv2.normalize(rgb_slice, None, 0, 255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
masks = mask_generator.generate(image)
img_crops = get_crops(image, masks, "crops")
# ----- CLIP Prompts -----
with open(config.clip_prompts, "r") as file:
prompts = json.load(file)
scores, _ = retrieve_relevant_crop(img_crops,prompts, model, preprocess, config)
# ------ bbox prompts cordinates relevant to ROI for SAM------
max_index = list(scores.values())[0]
bbox = masks[max_index]["bbox"]
bbox = np.array([bbox[0] , bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])
return bbox
def get_eval_3d(dataset, sam, config, suffix, wandb_mode):
folder_time = datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p")
wandb_run = wandb.init( project='SAM', entity='sidra-aleem2', name = config['model_name'] + "_" + suffix +"_"+ folder_time, mode = wandb_mode)
# ----- loading the models -----
clip_model, preprocess = clip.load("ViT-B/32", device="cuda")
resize_transform = ResizeLongestSide(sam.image_encoder.img_size)
sam.to("cuda")
mask_generator = sam_mask_generator_params(sam, clip_model, preprocess, resize_transform, dataset, config)
avg_dice = []
# ----- Inference -----
with torch.no_grad():
for idx in tqdm(range(len(dataset)), desc= f"Processing images", unit= "image"):
input_samples, gt_samples = dataset[idx]
slices = []
for _, img_slice in tqdm(enumerate(input_samples), total=len(input_samples), desc="Processing slices"): # looping over single img
bbox = get_bbox(img_slice, clip_model, preprocess, mask_generator, config ) # bbox from SAM+CLIP
# Format required by SAM predictor
batched_input = [
{
'image': prepare_image(img_slice, resize_transform, "cuda"),
'boxes': resize_transform.apply_boxes_torch(torch.from_numpy(bbox), img_slice.shape[:2]).to("cuda"),
'original_size':img_slice[0,:,:].shape
}]
preds = sam(batched_input, multimask_output=False)
slices.append(preds.squeeze().detach().cpu())
del preds
segmented_volume = torch.stack(slices, axis=0)
slices.clear()
mask = torch.zeros(segmented_volume.shape)
mask[torch.sigmoid(segmented_volume) > 0.5] = 1
del segmented_volume
test_dice = dice_coeff(mask, gt_samples.squeeze())
print("test dice",test_dice )
avg_dice.append(test_dice)
mask = mask.unsqueeze(1)
# logging images to wandb
log_images(input_samples, mask, gt_samples, "10" , "test", idx)
final_avg_dice = np.mean(avg_dice)
return final_avg_dice