Skip to content

Commit a75618f

Browse files
committed
Firsi Commit
0 parents  commit a75618f

File tree

6 files changed

+1942
-0
lines changed

6 files changed

+1942
-0
lines changed

Dice_loss.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import numpy as np
2+
from _23May2019_Unet.UNET_ import U_Net
3+
from _23May2019_Unet.VGG_segmentation import VGG_
4+
from keras import backend as K
5+
import keras
6+
from tqdm import tqdm
7+
from final_post_processing.Algorithm import algorithm
8+
# import mxnet as nd
9+
10+
########################################################################
11+
12+
width=512
13+
height=512
14+
depth=3
15+
classes=2
16+
data_shape=width*height
17+
########################################################################
18+
x_train=np.load('E:/Cell_data/x_train.npy')
19+
x_test=np.load('E:/Cell_data/x_test.npy')
20+
y_test=np.load('E:/Cell_data/y_test.npy')
21+
y_train=np.load('E:/Cell_data/y_train.npy')
22+
y_test = np.reshape(y_test,(len(y_test),data_shape,classes))
23+
y_train = np.reshape(y_train,(len(y_train),data_shape,classes))
24+
y=np.load('E:/Cell_data/label_cell.npy')
25+
y=np.reshape(y,(y.shape[0],y.shape[1]*y.shape[2],y.shape[3]))
26+
x_whitened=np.load('E:/Cell_data/data_cell.npy')
27+
28+
29+
########################################################################
30+
def dice_loss(y_true,y_pred):
31+
y_true=y_true.astype('float32')
32+
y_pred=y_pred.astype('float32')
33+
smooth=1
34+
intersection=2*sum(y_true*y_pred)
35+
union=sum(y_true)+sum(y_pred)
36+
if np.mean(union)==0 and np.mean(intersection)==0:
37+
dice_coef=0
38+
else:
39+
dice_coef=np.mean((intersection+smooth)/(union+smooth))
40+
dice_loss_=1-dice_coef
41+
return dice_loss_
42+
43+
44+
45+
46+
47+
48+
# def give_y(net,x):
49+
# z=[]
50+
# z.append(x)
51+
# a=net.predict(np.array(z))
52+
# a=np.reshape(a,(data_shape,classes))
53+
# a=a.astype('uint8')
54+
# return a
55+
#
56+
# def give_dice(x_test_whitened,y_test,net):
57+
# l=len(y_test)
58+
# dice=[]
59+
# for i in tqdm(range(l)):
60+
# y=give_y(net,x_test_whitened[i])
61+
# # print('giv_y is : ',y)
62+
# d=dice_loss(y, y_test[i])
63+
# dice.append(d)
64+
# return dice
65+
66+
67+
# #############################################################################
68+
# #UNet
69+
# pathtoweight1='E:/Cell_data/U-Net/'
70+
# K.set_image_data_format('channels_first')
71+
# net = U_Net(width, height, depth, classes,weightsPath=pathtoweight1+'Cell.h5_0183-0.9988.h5')
72+
# print('here')
73+
# dice_unet=give_dice(x_whitened, y, net)
74+
# #
75+
# mean_dice_unet=np.mean(dice_unet)
76+
# print('mean_dice_unet : ',mean_dice_unet)
77+
# # #########################################################################
78+
# #VGG
79+
# pathtoweight2='E:/Cell_data/VGG/'
80+
# model=VGG_(weightsPath=pathtoweight2+'Cell.h5_0008-0.9979.h5')
81+
#
82+
# dice_vgg=give_dice(x_whitened, y, model)
83+
# mean_dice_vgg=np.mean(dice_vgg)
84+
# print('mean_dice_vgg : ',mean_dice_vgg)
85+
#
86+
# ###########################################################################
87+
# #POST-PROCESSED
88+
# y_processed=np.load('E:/Cell_data/U-Net/Post-processing/final/images_categorical.npy')
89+
# dice_processed=[]
90+
# for i in tqdm(range(len(y_processed))):
91+
# d=dice_loss(y[i],y_processed[i])
92+
# dice_processed.append(d)
93+
# mean_dice_processed=np.mean(np.array(dice_processed))
94+
# print('mean_dice_processed : ',mean_dice_processed)
95+
###############################################################################
96+
#SUPER-PIXELS
97+
y_superpixels=np.load('E:/Cell_data/Superpixels/images_categorical.npy')
98+
dice_superpixels=[]
99+
for i in tqdm(range(len(y_superpixels))):
100+
# print(i)
101+
d=dice_loss(y[i],y_superpixels[i])
102+
dice_superpixels.append(d)
103+
mean_dice_superpixels=np.mean(np.array(dice_superpixels))
104+
print('mean_dice_superpixels : ',mean_dice_superpixels)
105+
print('done')
106+
107+
108+
109+
110+
111+
112+

0 commit comments

Comments
 (0)