MCPcopy Create free account
hub / github.com/ImprintLab/Medical-SAM2 / validation_sam

Function validation_sam

func_3d/function.py:198–314  ·  view source on GitHub ↗
(args, val_loader, epoch, net: nn.Module, clean_dir=True)

Source from the content-addressed store, hash-verified

196 return epoch_loss / len(train_loader), epoch_prompt_loss / len(train_loader), epoch_non_prompt_loss / len(train_loader)
197
198def validation_sam(args, val_loader, epoch, net: nn.Module, clean_dir=True):
199 # eval mode
200 net.eval()
201
202 n_val = len(val_loader) # the number of batch
203 mix_res = (0,)*1*2
204 tot = 0
205 threshold = (0.1, 0.3, 0.5, 0.7, 0.9)
206 prompt_freq = args.prompt_freq
207
208 lossfunc = criterion_G
209 # lossfunc = paper_loss
210
211 prompt = args.prompt
212
213 with tqdm(total=n_val, desc='Validation round', unit='batch', leave=False) as pbar:
214 for pack in val_loader:
215 imgs_tensor = pack['image']
216 mask_dict = pack['label']
217 if prompt == 'click':
218 pt_dict = pack['pt']
219 point_labels_dict = pack['p_label']
220 elif prompt == 'bbox':
221 bbox_dict = pack['bbox']
222 if len(imgs_tensor.size()) == 5:
223 imgs_tensor = imgs_tensor.squeeze(0)
224 frame_id = list(range(imgs_tensor.size(0)))
225
226 train_state = net.val_init_state(imgs_tensor=imgs_tensor)
227 prompt_frame_id = list(range(0, len(frame_id), prompt_freq))
228 obj_list = []
229 for id in frame_id:
230 obj_list += list(mask_dict[id].keys())
231 obj_list = list(set(obj_list))
232 if len(obj_list) == 0:
233 continue
234
235 name = pack['image_meta_dict']['filename_or_obj']
236
237 with torch.no_grad():
238 for id in prompt_frame_id:
239 for ann_obj_id in obj_list:
240 try:
241 if prompt == 'click':
242 points = pt_dict[id][ann_obj_id].to(device=GPUdevice)
243 labels = point_labels_dict[id][ann_obj_id].to(device=GPUdevice)
244 _, _, _ = net.train_add_new_points(
245 inference_state=train_state,
246 frame_idx=id,
247 obj_id=ann_obj_id,
248 points=points,
249 labels=labels,
250 clear_old_points=False,
251 )
252 elif prompt == 'bbox':
253 bbox = bbox_dict[id][ann_obj_id]
254 _, _, _ = net.train_add_new_bbox(
255 inference_state=train_state,

Callers

nothing calls this directly

Calls 7

eval_segFunction · 0.90
val_init_stateMethod · 0.80
train_add_new_pointsMethod · 0.80
train_add_new_bboxMethod · 0.80
train_add_new_maskMethod · 0.80
propagate_in_videoMethod · 0.80
reset_stateMethod · 0.80

Tested by

no test coverage detected