Switch to unified view

a b/DATA_PROCESS/Results_Plot.py
1
import numpy as np
2
import matplotlib.pyplot as plt
3
import pylab
4
import joblib
5
import cPickle as cp
6
# t = np.arange(0, 5, 0.2)
7
# t2 = np.arange(0, 5, 0.02)
8
9
# def f(t):
10
# return np.exp(-t)*np.cos(2*np.pi*t)
11
def dice_np(y_true, y_pred):
12
    y_true = y_true.reshape(y_true.shape[0], -1)
13
    y_pred = y_pred.reshape(y_pred.shape[0], -1)
14
15
    #y_true = np.reshape(y_true, -1)
16
    #y_pred = np.reshape(y_pred, -1)
17
    # y_true = y_true/np.max(np.max(y_true))
18
    # y_pred = y_pred/np.max(np.max(y_pred))
19
20
    #y_true[y_true > 0.0] = 1.0
21
    #y_pred[y_pred > 0.0] = 1.0
22
23
24
    print('Shapes  : ', y_true.shape, y_pred.shape)
25
    intersection = y_true*y_pred
26
27
    #print('Int shape ', intersection.shape)
28
    intersection = np.sum(intersection, axis = 1)
29
    #print('Int shape new ', intersection.shape)
30
    dr1 = np.sum(y_true, axis=1)
31
    dr2 = np.sum(y_pred, axis=1)
32
33
    #print('Dr ', dr1, dr2)
34
    dr = dr1+dr2
35
    nr = 2*intersection
36
37
38
    x = nr/dr
39
    return np.mean(x)
40
41
results = np.load('data.npz')
42
43
print(results.files)
44
45
print(results['imgs_test_X'].shape, results['imgs_test_Y'].shape, results['imgs_test_Pred'].shape)
46
47
# for i in range(0,results['imgs_test_X'].shape[0]):
48
#   plt.figure(1)
49
#   #plt.axis('off')
50
#   plt.title('%d"'%(i))
51
#   plt.subplot(231)
52
#   plt.imshow(results['imgs_test_X'][i][0])
53
#   plt.subplot(232)
54
#   plt.imshow(results['imgs_test_X'][i][0]*results['imgs_test_Y'][i][0], vmin = np.min(results['imgs_test_X'][i][0]), vmax = np.max(results['imgs_test_X'][i][0]))
55
#   plt.subplot(233)
56
#   plt.imshow(results['imgs_test_X'][i][0]*results['imgs_test_Pred'][i][0], vmin = np.min(results['imgs_test_X'][i][0]), vmax = np.max(results['imgs_test_X'][i][0]))
57
#   plt.subplot(235)
58
#   plt.imshow(results['imgs_test_Y'][i][0])
59
#   plt.subplot(236)
60
#   plt.imshow(results['imgs_test_Pred'][i][0])
61
#   pylab.show()
62
63
64
#weight = joblib.load(('weights'))
65
weight = cp.load(open('filters.pkl'))
66
67
#weight.reshape(weight.shape[0], -1)
68
69
#print('Weights : ', weight.keys())
70
#plt.figure(3)
71
#plt.subplot(filters_per_layer)
72
73
for key in (weight):
74
    if key.startswith('conv_') and key.endswith('_2'):
75
        print(key, len(weight[key]))
76
        for j in range(0, len(weight[key])):
77
                #weight[key].reshape(6)
78
            plt.figure(2)
79
            plt.subplot(1,2,1)
80
            plt.imshow(weight[key][0])
81
            plt.axis('off')
82
            plt.subplot(1,2,2)
83
            plt.imshow(weight[key][1])
84
            plt.axis('off')
85
            plt.figure(3)
86
            plt.subplot(1,2,1)
87
            plt.imshow(weight[key][2])
88
            plt.axis('off')
89
            plt.subplot(1,2,2)
90
            plt.imshow(weight[key][3])
91
            plt.axis('off')
92
        pylab.show()
93
94
95
def plot_3d(image, threshold=-300):
96
    
97
    # Position the scan upright, 
98
    # so the head of the patient would be at the top facing the camera
99
    p = image.transpose(2,1,0)
100
    p = p[:,:,::-1]
101
    
102
    verts, faces = measure.marching_cubes(p, threshold)
103
104
    fig = plt.figure(figsize=(10, 10))
105
    ax = fig.add_subplot(111, projection='3d')
106
107
    # Fancy indexing: `verts[faces]` to generate a collection of triangles
108
    mesh = Poly3DCollection(verts[faces], alpha=0.1)
109
    face_color = [0.5, 0.5, 1]
110
    mesh.set_facecolor(face_color)
111
    ax.add_collection3d(mesh)
112
113
    ax.set_xlim(0, p.shape[0])
114
    ax.set_ylim(0, p.shape[1])
115
    ax.set_zlim(0, p.shape[2])
116
117
    plt.show()
118
119
120
#plots = np.zeros(results['imgs_test_Y'].shape[0],results['imgs_test_Y'].shape[2],results['imgs_test_Y'].shape[3])
121
122
#print(plots.shape)
123
124
#for i in range(0, results['imgs_test_Y'].shape[0]):
125
126
127
#plot_3d(results['imgs_test_Y'][])
128
129
130
####### PLOTTING
131
# plot_res = results['imgs_test_X'][0][0][0:200, 300:500]
132
# #*results['imgs_test_Y'][0][0][300:500]
133
# print('Shape : ', plot_res.shape)
134
135
ind = 19
136
# plt.figure(1)
137
# plt.title('Lung CT Scan')
138
# plt.imshow(results['imgs_test_X'][ind][0])
139
# plt.axis('off')
140
# #pylab.show()
141
142
# Im1 = results['imgs_test_Y'][ind][0]
143
# Im2 = results['imgs_test_Pred'][ind][0]
144
145
# print(dice_np(Im1, Im2))
146
147
# plt.figure(2)
148
# plt.title('Results')
149
# ax1 = plt.subplot(131)
150
# plt.imshow(results['imgs_test_X'][ind][0][150:350, 300:500], vmin = np.min(results['imgs_test_X'][ind][0]), vmax = np.max(results['imgs_test_X'][ind][0]))
151
# ax1.set_title('Region of Interest')
152
# plt.axis('off')
153
# ax1 = plt.subplot(132)
154
# plt.imshow(results['imgs_test_X'][ind][0][150:350, 300:500]*results['imgs_test_Y'][ind][0][150:350, 300:500], vmin = np.min(results['imgs_test_X'][ind][0]), vmax = np.max(results['imgs_test_X'][ind][0]))
155
# ax1.set_title('Gold Standard Mask')
156
# plt.axis('off')
157
# ax2 = plt.subplot(133)
158
# plt.imshow(results['imgs_test_X'][ind][0][150:350, 300:500]*results['imgs_test_Pred'][ind][0][150:350, 300:500], vmin = np.min(results['imgs_test_X'][ind][0]), vmax = np.max(results['imgs_test_X'][ind][0]))
159
# ax2.set_title('Predicted Mask')
160
# plt.axis('off')
161
# pylab.show()
162
163
###### PLOTTING END
164
165
166
167
168
169
170
# testMasks = np.load('testMasks.npy')
171
# trainedMasks = np.load('masksTestPredicted.npy')
172
# testIm = np.load('testImages.npy')
173
# trainIm = np.load('trainImages.npy')
174
# trainMasks = np.load('trainMasks.npy')
175
# print(testMasks.shape)
176
# print(trainedMasks.shape)
177
# print(testIm.shape)
178
# print(trainIm.shape)
179
# print(trainMasks.shape)
180
181
# for i in range(0,testIm.shape[0]):
182
#   plt.figure(1)
183
#   plt.subplot(231)
184
#   plt.imshow(testIm[i][0])
185
#   plt.subplot(232)
186
#   plt.imshow(testMasks[i][0])
187
#   plt.subplot(234)
188
#   plt.imshow(testIm[i][0]*testMasks[i][0])
189
#   plt.subplot(233)
190
#   plt.imshow(trainedMasks[i][0])
191
#   plt.subplot(235)
192
#   plt.imshow(testIm[i][0]*trainedMasks[i][0])
193
#   pylab.show()
194
195
# for i in range(0,trainIm.shape[0]):
196
#   plt.figure(1)
197
#   plt.subplot(131)
198
#   plt.imshow(trainIm[i][0])
199
#   plt.subplot(132)
200
#   plt.imshow(trainMasks[i][0])
201
#   plt.subplot(133)
202
#   plt.imshow(trainMasks[i][0]*trainIm[i][0])
203
#   pylab.show()
204
205
# print(np.sum(trainIm[0][0]), np.sum(trainIm[1][0]))
206
# for i in range(0,trainIm.shape[0]):
207
#   plt.figure(2)
208
#   plt.subplot(121)
209
#   plt.imshow(trainIm[i][0])
210
#   plt.subplot(122)
211
#   plt.imshow(trainMasks[i][0]*trainIm[i][0])
212
#   pylab.show()
213
# for i in range(0,trainedMasks.shape[0]):
214
#   plt.figure(i+1)
215
#   plt.imshow(trainedMasks[i][0])
216
#   pylab.show()
217
# for i in range(0,testMasks.shape[0]):
218
#   plt.figure(i+1)
219
#   plt.imshow(testMasks[i][0])
220
#   pylab.show()
221
# plt.figure(1)
222
# plt.subplot(211)
223
# plt.plot(t, f(t), 'bo', t2, f(t2), 'k')
224
225
# plt.subplot(212)
226
# plt.plot(t2, np.cos(2*np.pi*t2), 'k')
227
228
# pylab.show()