forked from SuperMedIntel/Medical-SAM-Adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
val.py
126 lines (104 loc) · 3.82 KB
/
val.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
# train.py
#!/usr/bin/env python3
""" valuate network using pytorch
Junde Wu
"""
import os
import sys
import argparse
from datetime import datetime
from collections import OrderedDict
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.metrics import roc_auc_score, accuracy_score,confusion_matrix
import torchvision
import torchvision.transforms as transforms
from skimage import io
from torch.utils.data import DataLoader
#from dataset import *
from torch.autograd import Variable
from PIL import Image
from tensorboardX import SummaryWriter
#from models.discriminatorlayer import discriminator
from dataset import *
from conf import settings
import time
import cfg
from tqdm import tqdm
from torch.utils.data import DataLoader, random_split
from utils import *
import function
def main():
args = cfg.parse_args()
if args.dataset == 'refuge' or args.dataset == 'refuge2':
args.data_path = '../dataset'
use_gpu = not args.no_gpu # if set -no_gpu, it will set use_gpu to False
if use_gpu == True:
GPUdevice = torch.device('cuda', args.gpu_device)
else:
GPUdevice = torch.device('cpu')
net = get_network(args, args.net, use_gpu, gpu_device=GPUdevice, distribution = args.distributed)
if args.weights != 0:
'''load adapted model'''
assert args.weights != 0
print(f'=> resuming from {args.weights}')
assert os.path.exists(args.weights)
checkpoint_file = os.path.join(args.weights)
assert os.path.exists(checkpoint_file)
if use_gpu:
loc = 'cuda:{}'.format(args.gpu_device)
else:
loc = 'cpu'
checkpoint = torch.load(checkpoint_file, map_location=loc)
start_epoch = checkpoint['epoch']
best_tol = checkpoint['best_tol']
state_dict = checkpoint['state_dict']
else :
'''load pretrained model'''
checkpoint_file = os.path.join(args.sam_ckpt)
assert os.path.exists(checkpoint_file)
loc = 'cuda:{}'.format(args.gpu_device)
checkpoint = torch.load(checkpoint_file, map_location=loc)
start_epoch = 0
state_dict = checkpoint
if args.distributed != 'none':
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
# name = k[7:] # remove `module.`
name = 'module.' + k
new_state_dict[name] = v
# load params
else:
new_state_dict = state_dict
net.load_state_dict(new_state_dict)
writer = SummaryWriter(log_dir=os.path.join(
settings.LOG_DIR, args.net, settings.TIME_NOW))
args.path_helper = set_log_dir('logs', args.exp_name)
logger = create_logger(args.path_helper['log_path'])
logger.info(args)
print(f'=> loaded checkpoint {checkpoint_file} (epoch {start_epoch})')
'''segmentation data'''
nice_train_loader, nice_test_loader = get_dataloader(args)
# set the model to evaluation mode
net.eval()
# start evaluation
time_start = time.time()
logger.info("Starting evaluation...")
if args.dataset != 'REFUGE':
tol, (eiou, edice) = function.validation_sam(args, nice_test_loader, start_epoch, net, writer)
logger.info(f'Evaluation completed. Total score: {tol}, IOU: {eiou}, DICE: {edice}.')
else:
tol, (eiou_cup, eiou_disc, edice_cup, edice_disc) = function.validation_sam(args, nice_test_loader, start_epoch, net, writer)
logger.info(
f'Evaluation completed. Total score: {tol}, '
f'IOU_CUP: {eiou_cup}, IOU_DISC: {eiou_disc}, '
f'DICE_CUP: {edice_cup}, DICE_DISC: {edice_disc}.'
)
time_end = time.time()
logger.info(f"Evaluation time: {time_end - time_start:.2f} seconds.")
writer.close()
if __name__ == '__main__':
main()