-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy patheval_all.py
More file actions
133 lines (100 loc) · 3.58 KB
/
eval_all.py
File metadata and controls
133 lines (100 loc) · 3.58 KB
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
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import sys
import torch.backends.cudnn as cudnn
from torch import optim
from optparse import OptionParser
from torch.autograd import Variable
from myloss import dice_coeff
from utils import *
from unet import UNet
def eval_net(net, dataset, gpu=False):
tot = 0
for i, b in enumerate(dataset):
X = b[0]
y = b[1]
X = torch.FloatTensor(X).unsqueeze(0)
y = torch.ByteTensor(y).unsqueeze(0)
if gpu:
X = Variable(X, volatile=True).cuda()
y = Variable(y, volatile=True).cuda()
else:
X = Variable(X, volatile=True)
y = Variable(y, volatile=True)
y_pred = net(X)
y_pred = (F.sigmoid(y_pred) > 0.6).float()
dice = dice_coeff(y_pred, y.float()).data[0]
tot += dice
if 0:
X = X.data.squeeze(0).cpu().numpy()
X = np.transpose(X, axes=[1, 2, 0])
y = y.data.squeeze(0).cpu().numpy()
y_pred = y_pred.data.squeeze(0).squeeze(0).cpu().numpy()
print(y_pred.shape)
fig = plt.figure()
ax1 = fig.add_subplot(1, 4, 1)
ax1.imshow(X)
ax2 = fig.add_subplot(1, 4, 2)
ax2.imshow(y)
ax3 = fig.add_subplot(1, 4, 3)
ax3.imshow((y_pred > 0.5))
Q = dense_crf(((X * 255).round()).astype(np.uint8), y_pred)
ax4 = fig.add_subplot(1, 4, 4)
print(Q)
ax4.imshow(Q > 0.5)
plt.show()
print("i=",i)
return tot / i
def Dice_Coeff(net, batch_size=2, lr=0.02, val_percent=1,
cp=True, gpu=False):
dir_img = '/home/wdh/DataSets/hand-segmentation/GTEA_gaze_part/Resize/Images/'
dir_mask = '/home/wdh/DataSets/hand-segmentation/GTEA_gaze_part/Resize/Masks_1/'
ids = get_ids(dir_img)
ids = split_ids(ids)
iddataset = split_train_val(ids, val_percent)
print('''
Starting evaluating:
Batch size: {}
Learning rate: {}
Training size: {}
Validation size: {}
CUDA: {}
'''.format(batch_size, lr, len(iddataset['train']),
len(iddataset['val']), str(gpu)))
N_train = len(iddataset['train'])
criterion = nn.BCELoss()
# reset the generators
val = get_imgs_and_masks(iddataset['val'], dir_img, dir_mask)
if 1:
evaluate_dice = eval_net(net, val, gpu)
print('Dice Coeff for all: {}'.format(evaluate_dice))
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-b', '--batch-size', dest='batchsize', default=2,
type='int', help='batch size')
parser.add_option('-l', '--learning-rate', dest='lr', default=0.02,
type='float', help='learning rate')
parser.add_option('-g', '--gpu', action='store_true', dest='gpu',
default=False, help='use cuda')
parser.add_option('-c', '--load', dest='load',
default=False, help='load file model')
(options, args) = parser.parse_args()
net = UNet(3, 1)
if options.load:
net.load_state_dict(torch.load(options.load))
print('Model loaded from {}'.format(options.load))
if options.gpu:
net.cuda()
cudnn.benchmark = True
try:
Dice_Coeff(net,options.batchsize, options.lr,
gpu=options.gpu)
except KeyboardInterrupt:
print('interrupt')
try:
sys.exit(0)
except SystemExit:
os._exit(0)