def save_model_and_predict():
	print 'Saving model...'
	save_file = file('CNNmodel.pkl', 'wb')
	genVariables = [epoch,pat_idx,tim_idx,xi,yi,zi+numPred,itr+1,patience,best_validation_loss]
	cPickle.dump(genVariables,save_file,protocol = cPickle.HIGHEST_PROTOCOL) 
	for i in xrange(len(params)):
		cPickle.dump(params[i].get_value(borrow=True), save_file, protocol = cPickle.HIGHEST_PROTOCOL)    
	save_file.close()
	for l in logcost:
		myfile.write("%f\n"%l)
	logcost = []

	print('Predicting for test patient')
	
	tpat = ReadPat.new(test_pat_num,test_tstamp,num_channels)
	shared_data.set_value(numpy.asarray(tpat.data,dtype = theano.config.floatX),borrow = True)
	shared_truth.set_values(numpy.asarray(tpat.truth,dtype = 'int32'),borrow = True)

	pstart_time = time.clock()
	Prediction = numpy.zeros(img_shape)

	for ix in xrange(len(xvalues)):
		for iy in xrange(len(yvalues)):
				for iz in xrange(len(zvalues)):
					errors,p_y_given_x,pred = test_model(xvalues[ix],yvalues[iy],zvalues[iz])
					pred = pred.reshape([numPred,numPred,numPred])
					Prediction[xvalues[ix]+offset/2:xvalues[ix]+plen-offset/2 +1,
							   yvalues[iy]+offset/2:yvalues[iy]+plen-offset/2 +1,
							   zvalues[iz]+offset/2:zvalues[iz]+plen-offset/2 +1] = pred
					p_y_given_x = p_y_given_x.transpose(1,0).reshape([num_classes,numPred,numPred,numPred])
					PostProbs[:,xvalues[ix]+offset/2:xvalues[ix]+plen-offset/2 +1,
							   	yvalues[iy]+offset/2:yvalues[iy]+plen-offset/2 +1,
							   	zvalues[iz]+offset/2:zvalues[iz]+plen-offset/2 +1] = p_y_given_x
def load_shareddata(pat_idx):
	data = numpy.zeros(TStamps[pat_idx],num_channels, img_shape[0],img_shape[1],img_shape[2]],dtype = theano.config.floatX)
	truth = numpy.zeros(TStamps[pat_idx],img_shape[0], img_shape[1],img_shape[2]],dtype = 'int32')
	for index in xrange(TStamps[pat_idx]):
		pat = ReadPat.new(pat_idx+1,index+1,num_channels)
		data[index,:,:,:,:] = numpy.asarray(pat.data,dtype = theano.config.floatX)
		truth[index,:,:,:] = numpy.asarray(pat.truth,dtype = 'int32')				
	shared_data.set_value(data)
	truth_truth.set_value(truth)
x = (20,160)
y = (17,192)
z = (11,151)
img_shape = [181,217,181]
num_patients = 4
TStamps = [4,4,5,4]

patch_size = [19,19]

valid_pat_num = 5
valid_Tstamps = 4

test_pat_num = 1
test_Tstamp = 1

pat = readPatMS.new(1,1)
num_patches = np.shape(image.extract_patches_2d(pat.data[0,20:160,17:192,0],patch_size))[0]

train_patches = np.zeros([num_patches, num_channels, patch_size[0], patch_size[1]])
trpatches_truth = np.zeros([num_patches])

shared_data = theano.shared(numpy.asarray(train_patches,dtype = theano.config.floatX),borrow = True)
shared_truth = theano.shared(numpy.asarray(trpatches_truth,dtype = 'int32'),borrow = True)

rng = numpy.random.RandomState(23455)

#Define Theano Tensors
nz = T.lscalar()
x = T.ftensor4('x')  
y = T.ivector('y')  
	epoch,pat_idx,tim_idx,xi,yi,zi,itr,patience,best_validation_loss = [0,0,0,0,0,0,0,patience,numpy.inf]
	layer3convW = None
	layer3convb = None
	layer2convW = None
	layer2convb = None
	layer1convW = None
	layer1convb = None
	layer0convW = None
	layer0convb = None
######################################
##Loading Dataset to Shared space

data = numpy.zeros([numpy.max(TStamps),num_channels,img_shape[0],img_shape[1],img_shape[2]])
truth = numpy.zeros([numpy.max(TStamps),img_shape[0],img_shape[1],img_shape[2]])
for ix in xrange(TStamps[0]):
	pat = ReadPat.new(1,ix+1,num_channels)
	data[ix,:,:,:,:] = pat.data
	truth[ix,:,:,:] = pat.truth
shared_data = theano.shared(numpy.asarray(data,dtype = theano.config.floatX),borrow = True)
shared_truth = theano.shared(numpy.asarray(truth,dtype = 'int32'),borrow = True)

shape = tpat.truth.shape
Prediction = numpy.zeros(shape)
shape = numpy.insert(shape,0,num_classes)
PostProbs = numpy.zeros(shape)

######################################
##Build architecture
p_shape = (plen,plen,plen)
#ignore_border = True
idx = T.lscalar()
Exemple #5
0
savedModel = file('CNNmodel.pkl', 'rb')
genVariables = cPickle.load(savedModel)
epoch, pat_idx, tim_idx, ix, iy, iz, itr, best_validation_loss, best_itr = genVariables
layer3convW = cPickle.load(savedModel)
layer3convb = cPickle.load(savedModel)
layer2convW = cPickle.load(savedModel)
layer2convb = cPickle.load(savedModel)
layer1convW = cPickle.load(savedModel)
layer1convb = cPickle.load(savedModel)
layer0convW = cPickle.load(savedModel)
layer0convb = cPickle.load(savedModel)

######################################
##Loading Dataset to Shared space

tpat = readPatMS.new(test_pat_num, test_tstamp)
test_data = theano.shared(numpy.asarray(tpat.data, dtype=theano.config.floatX),
                          borrow=True)
test_truth = theano.shared(numpy.asarray(tpat.truth, dtype='int32'),
                           borrow=True)

p_shape = (plen, plen, plen)
#ignore_border = True
idx = T.lscalar()
idy = T.lscalar()
idz = T.lscalar()
time_idx = T.lscalar()

x = T.ftensor4('x')
z = T.itensor3('y')
y = z.reshape([