-
Notifications
You must be signed in to change notification settings - Fork 122
/
VNet.py
198 lines (140 loc) · 8.08 KB
/
VNet.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import caffe
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import os
import DataManager as DM
import utilities
from os.path import splitext
from multiprocessing import Process, Queue
class VNet(object):
params=None
dataManagerTrain=None
dataManagerTest=None
def __init__(self,params):
self.params=params
caffe.set_device(self.params['ModelParams']['device'])
caffe.set_mode_gpu()
def prepareDataThread(self, dataQueue, numpyImages, numpyGT):
nr_iter = self.params['ModelParams']['numIterations']
batchsize = self.params['ModelParams']['batchsize']
keysIMG = numpyImages.keys()
nr_iter_dataAug = nr_iter*batchsize
np.random.seed()
whichDataList = np.random.randint(len(keysIMG), size=int(nr_iter_dataAug/self.params['ModelParams']['nProc']))
whichDataForMatchingList = np.random.randint(len(keysIMG), size=int(nr_iter_dataAug/self.params['ModelParams']['nProc']))
for whichData,whichDataForMatching in zip(whichDataList,whichDataForMatchingList):
filename, ext = splitext(keysIMG[whichData])
currGtKey = filename + '_segmentation' + ext
currImgKey = filename + ext
# data agugumentation through hist matching across different examples...
ImgKeyMatching = keysIMG[whichDataForMatching]
defImg = numpyImages[currImgKey]
defLab = numpyGT[currGtKey]
defImg = utilities.hist_match(defImg, numpyImages[ImgKeyMatching])
if(np.random.rand(1)[0]>0.5): #do not apply deformations always, just sometimes
defImg, defLab = utilities.produceRandomlyDeformedImage(defImg, defLab,
self.params['ModelParams']['numcontrolpoints'],
self.params['ModelParams']['sigma'])
weightData = np.zeros_like(defLab,dtype=float)
weightData[defLab == 1] = np.prod(defLab.shape) / np.sum((defLab==1).astype(dtype=np.float32))
weightData[defLab == 0] = np.prod(defLab.shape) / np.sum((defLab == 0).astype(dtype=np.float32))
dataQueue.put(tuple((defImg,defLab, weightData)))
def trainThread(self,dataQueue,solver):
nr_iter = self.params['ModelParams']['numIterations']
batchsize = self.params['ModelParams']['batchsize']
batchData = np.zeros((batchsize, 1, self.params['DataManagerParams']['VolSize'][0], self.params['DataManagerParams']['VolSize'][1], self.params['DataManagerParams']['VolSize'][2]), dtype=float)
batchLabel = np.zeros((batchsize, 1, self.params['DataManagerParams']['VolSize'][0], self.params['DataManagerParams']['VolSize'][1], self.params['DataManagerParams']['VolSize'][2]), dtype=float)
#only used if you do weighted multinomial logistic regression
batchWeight = np.zeros((batchsize, 1, self.params['DataManagerParams']['VolSize'][0],
self.params['DataManagerParams']['VolSize'][1],
self.params['DataManagerParams']['VolSize'][2]), dtype=float)
train_loss = np.zeros(nr_iter)
for it in range(nr_iter):
for i in range(batchsize):
[defImg, defLab, defWeight] = dataQueue.get()
batchData[i, 0, :, :, :] = defImg.astype(dtype=np.float32)
batchLabel[i, 0, :, :, :] = (defLab > 0.5).astype(dtype=np.float32)
batchWeight[i, 0, :, :, :] = defWeight.astype(dtype=np.float32)
solver.net.blobs['data'].data[...] = batchData.astype(dtype=np.float32)
solver.net.blobs['label'].data[...] = batchLabel.astype(dtype=np.float32)
#solver.net.blobs['labelWeight'].data[...] = batchWeight.astype(dtype=np.float32)
#use only if you do softmax with loss
solver.step(1) # this does the training
train_loss[it] = solver.net.blobs['loss'].data
if (np.mod(it, 10) == 0):
plt.clf()
plt.plot(range(0, it), train_loss[0:it])
plt.pause(0.00000001)
matplotlib.pyplot.show()
def train(self):
print self.params['ModelParams']['dirTrain']
#we define here a data manage object
self.dataManagerTrain = DM.DataManager(self.params['ModelParams']['dirTrain'],
self.params['ModelParams']['dirResult'],
self.params['DataManagerParams'])
self.dataManagerTrain.loadTrainingData() #loads in sitk format
howManyImages = len(self.dataManagerTrain.sitkImages)
howManyGT = len(self.dataManagerTrain.sitkGT)
assert howManyGT == howManyImages
print "The dataset has shape: data - " + str(howManyImages) + ". labels - " + str(howManyGT)
test_interval = 50000
# Write a temporary solver text file because pycaffe is stupid
with open("solver.prototxt", 'w') as f:
f.write("net: \"" + self.params['ModelParams']['prototxtTrain'] + "\" \n")
f.write("base_lr: " + str(self.params['ModelParams']['baseLR']) + " \n")
f.write("momentum: 0.99 \n")
f.write("weight_decay: 0.0005 \n")
f.write("lr_policy: \"step\" \n")
f.write("stepsize: 20000 \n")
f.write("gamma: 0.1 \n")
f.write("display: 1 \n")
f.write("snapshot: 500 \n")
f.write("snapshot_prefix: \"" + self.params['ModelParams']['dirSnapshots'] + "\" \n")
#f.write("test_iter: 3 \n")
#f.write("test_interval: " + str(test_interval) + "\n")
f.close()
solver = caffe.SGDSolver("solver.prototxt")
os.remove("solver.prototxt")
if (self.params['ModelParams']['snapshot'] > 0):
solver.restore(self.params['ModelParams']['dirSnapshots'] + "_iter_" + str(
self.params['ModelParams']['snapshot']) + ".solverstate")
plt.ion()
numpyImages = self.dataManagerTrain.getNumpyImages()
numpyGT = self.dataManagerTrain.getNumpyGT()
#numpyImages['Case00.mhd']
#numpy images is a dictionary that you index in this way (with filenames)
for key in numpyImages:
mean = np.mean(numpyImages[key][numpyImages[key]>0])
std = np.std(numpyImages[key][numpyImages[key]>0])
numpyImages[key]-=mean
numpyImages[key]/=std
dataQueue = Queue(30) #max 50 images in queue
dataPreparation = [None] * self.params['ModelParams']['nProc']
#thread creation
for proc in range(0,self.params['ModelParams']['nProc']):
dataPreparation[proc] = Process(target=self.prepareDataThread, args=(dataQueue, numpyImages, numpyGT))
dataPreparation[proc].daemon = True
dataPreparation[proc].start()
self.trainThread(dataQueue, solver)
def test(self):
self.dataManagerTest = DM.DataManager(self.params['ModelParams']['dirTest'], self.params['ModelParams']['dirResult'], self.params['DataManagerParams'])
self.dataManagerTest.loadTestData()
net = caffe.Net(self.params['ModelParams']['prototxtTest'],
os.path.join(self.params['ModelParams']['dirSnapshots'],"_iter_" + str(self.params['ModelParams']['snapshot']) + ".caffemodel"),
caffe.TEST)
numpyImages = self.dataManagerTest.getNumpyImages()
for key in numpyImages:
mean = np.mean(numpyImages[key][numpyImages[key]>0])
std = np.std(numpyImages[key][numpyImages[key]>0])
numpyImages[key] -= mean
numpyImages[key] /= std
results = dict()
for key in numpyImages:
btch = np.reshape(numpyImages[key],[1,1,numpyImages[key].shape[0],numpyImages[key].shape[1],numpyImages[key].shape[2]])
net.blobs['data'].data[...] = btch
out = net.forward()
l = out["labelmap"]
labelmap = np.squeeze(l[0,1,:,:,:])
results[key] = np.squeeze(labelmap)
self.dataManagerTest.writeResultsFromNumpyLabel(np.squeeze(labelmap),key)