-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_unet_4.py
36 lines (32 loc) · 971 Bytes
/
test_unet_4.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
import torch
from torch.utils import data
from unet import Unet
from dataset_brain import Dataset_brain_4
from loss import dice_loss
from loss import dice_score
torch.backends.cudnn.benchmark=True
torch.manual_seed(10)
file_path='./brats18_dataset/npy_test/test_t2.npy'
model_path='./weight/unet_4.pth'
train_data=Dataset_brain_4(file_path)
batch_size=64
train_loader=data.DataLoader(dataset=train_data,batch_size=batch_size,num_workers=4)
unet=Unet(4)
unet.load_state_dict(torch.load(model_path))
unet.cuda()
unet.eval()
batch_score=0
num_batch=0
with torch.no_grad():
for i,(img,label) in enumerate(train_loader):
# print(img.shape)
seg=unet(img.float().cuda())
seg=seg.cpu()
seg[seg>=0.5]=1.
seg[seg!=1]=0.
batch_score+=dice_score(seg,label.float()).data.numpy()
# print(num_batch)
num_batch+=img.size(0)
del seg,img,label
batch_score/=num_batch
print(' test_score = %.4f'%(batch_score))