(args, val_loader, epoch, net: nn.Module, clean_dir=True)
| 257 | |
| 258 | |
| 259 | def validation_sam(args, val_loader, epoch, net: nn.Module, clean_dir=True): |
| 260 | |
| 261 | # use bfloat16 for the entire notebook |
| 262 | torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__() |
| 263 | |
| 264 | if torch.cuda.get_device_properties(0).major >= 8: |
| 265 | # turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices) |
| 266 | torch.backends.cuda.matmul.allow_tf32 = True |
| 267 | torch.backends.cudnn.allow_tf32 = True |
| 268 | |
| 269 | |
| 270 | # eval mode |
| 271 | net.eval() |
| 272 | |
| 273 | n_val = len(val_loader) |
| 274 | threshold = (0.1, 0.3, 0.5, 0.7, 0.9) |
| 275 | GPUdevice = torch.device('cuda:' + str(args.gpu_device)) |
| 276 | |
| 277 | # init |
| 278 | lossfunc = criterion_G |
| 279 | memory_bank_list = [] |
| 280 | feat_sizes = [(256, 256), (128, 128), (64, 64)] |
| 281 | total_loss = 0 |
| 282 | total_eiou = 0 |
| 283 | total_dice = 0 |
| 284 | |
| 285 | |
| 286 | with tqdm(total=n_val, desc='Validation round', unit='batch', leave=False) as pbar: |
| 287 | for ind, pack in enumerate(val_loader): |
| 288 | to_cat_memory = [] |
| 289 | to_cat_memory_pos = [] |
| 290 | to_cat_image_embed = [] |
| 291 | |
| 292 | name = pack['image_meta_dict']['filename_or_obj'] |
| 293 | imgs = pack['image'].to(dtype = torch.float32, device = GPUdevice) |
| 294 | masks = pack['mask'].to(dtype = torch.float32, device = GPUdevice) |
| 295 | |
| 296 | |
| 297 | if 'pt' in pack: |
| 298 | pt_temp = pack['pt'].to(device = GPUdevice) |
| 299 | pt = pt_temp.unsqueeze(1) |
| 300 | point_labels_temp = pack['p_label'].to(device = GPUdevice) |
| 301 | point_labels = point_labels_temp.unsqueeze(1) |
| 302 | coords_torch = torch.as_tensor(pt, dtype=torch.float, device=GPUdevice) |
| 303 | labels_torch = torch.as_tensor(point_labels, dtype=torch.int, device=GPUdevice) |
| 304 | else: |
| 305 | coords_torch = None |
| 306 | labels_torch = None |
| 307 | |
| 308 | |
| 309 | |
| 310 | '''test''' |
| 311 | with torch.no_grad(): |
| 312 | |
| 313 | """ image encoder """ |
| 314 | backbone_out = net.forward_image(imgs) |
| 315 | _, vision_feats, vision_pos_embeds, _ = net._prepare_backbone_features(backbone_out) |
| 316 | B = vision_feats[-1].size(1) |
nothing calls this directly
no test coverage detected