-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorig_mod_NNEval.py
141 lines (117 loc) · 4.02 KB
/
orig_mod_NNEval.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
import numpy as np
import pickle
import time
import os
import nibabel as nib
import sys
import glob
from scipy.io import loadmat
from scipy.io import savemat
import math
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#Comment in if there is a GPU/CUDA enabled device
#os.environ["CUDA_VISIBLE_DEVICES"]="0"
def conv_block(x,KERNEL_SIZE, FILTERS):
x = tf.layers.conv3d(x,FILTERS,KERNEL_SIZE,padding='same')
return tf.nn.elu(x)
def unet(x,N_CLASS):
with tf.name_scope('layer1_enc'):
x1 = conv_block(x,3,16)
x1 = conv_block(x1,3,16)
x1 = conv_block(x1,3,16)
x1p = tf.layers.max_pooling3d(x1, 2, 2)
print(x1p.shape)
with tf.name_scope('layer2_enc'):
x2 = conv_block(x1p,3,32)
x2 = conv_block(x2,3,32)
x2 = conv_block(x2,3,32)
x2p = tf.layers.max_pooling3d(x2, 2, 2)
print(x2p.shape)
with tf.name_scope('layer3_enc'):
x3 = conv_block(x2p,3,64)
x3 = conv_block(x3,3,64)
x3 = conv_block(x3,3,64)
x3p = tf.layers.max_pooling3d(x3, 2, 2)
print(x3p.shape)
with tf.name_scope('bottom'):
x4 = conv_block(x3p,3,128)
x4 = conv_block(x4,3,128)
x4 = conv_block(x4,3,128)
x4t = tf.layers.conv3d_transpose(x4,64,3,strides=2,padding='same')
print('p0')
print(x4t.shape)
with tf.name_scope('layer3_dec'):
x3 = tf.concat((x4t,x3),axis=-1)
x3 = conv_block(x3,3,64)
x3 = conv_block(x3,3,64)
x3 = conv_block(x3,3,64)
x3t = tf.layers.conv3d_transpose(x3,32,3,strides=2,padding='same')
print(x3t.shape)
with tf.name_scope('layer2_dec'):
x2 = tf.concat((x3t,x2),axis=-1)
x2 = conv_block(x2,3,32)
x2 = conv_block(x2,3,32)
x2 = conv_block(x2,3,32)
x2t = tf.layers.conv3d_transpose(x2,16,3,strides=2,padding='same')
with tf.name_scope('layer1_dec'):
x1 = tf.concat((x2t,x1),axis=-1)
x1 = conv_block(x1,3,16)
x1 = conv_block(x1,3,16)
x1 = conv_block(x1,3,16)
x1 = tf.layers.conv3d(x1, N_CLASS, 1)
return(x1)
#new
#data directory for cnn project
#data_dir='/mnt/jxvs2_02/neil/Striatal_Segmentation/Data_01/'
# outdated - ignore below
cluster=1
#this is the path to the T1 skull-stripped image
testList=[sys.argv[1]]
launch=os.getcwd()
#tf.reset_default_graph()
#os.environ["CUDA_VISIBLE_DEVICES"]="0"
X = tf.placeholder(tf.float32, shape=[1, 256, 256, 192, 1], name='X') #input
pred = unet(X, 6)
new_saver=tf.train.Saver()
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# the below is where the network weights are located provided by user input
print(sys.argv[3])
new_saver.restore(sess,tf.train.latest_checkpoint(sys.argv[3]))
print("Loaded Weights")
#for i in testList:
#changed below because it did not finish entire list
for i in range(len(testList)):
#new
print(i)
out_dict={}
# we directly import the t1 image here below
mri=sys.argv[1]
mri=nib.load(mri)
mri=mri.get_data()
print(mri.shape) # original mri dimensions before any transformations
mri_out=mri
mri=np.expand_dims(mri,0)
if mri.shape[-1]!=1:
mri=np.expand_dims(mri,-1)
padder1=192-mri.shape[3]
padder1=np.zeros((mri.shape[1],mri.shape[2],padder1))
padder1=np.expand_dims(padder1,0)
padder1=np.expand_dims(padder1,-1)
mri=np.concatenate((padder1,mri),axis=3)
print(mri.shape)
padder2=np.zeros((mri.shape[0],max(0,256-mri.shape[1]),mri.shape[2],mri.shape[3],1))
mri=np.concatenate((padder2,mri),axis=1)
padder3=np.zeros((mri.shape[0],mri.shape[1],max(0,256-mri.shape[2]),mri.shape[3],1))
mri=np.concatenate((padder3,mri),axis=2)
print(mri.shape)
outt=np.zeros((mri.shape[0],mri.shape[1],mri.shape[2]))
b = sess.run(pred,feed_dict={X:mri})
b=sess.run(tf.nn.softmax(b))
out_dict['out']=b
out_dict['mri']=mri_out
print(os.getcwd())
savedir=sys.argv[2]
savemat(savedir,out_dict)
print('python script complete.')