forked from WU-C2IR2/DR2UNet-for-TNBC-PDX-Tumor-Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dense_unet.py
124 lines (92 loc) · 4.76 KB
/
dense_unet.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 2 09:37:19 2020
@author: kdutta01
"""
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv2D, Conv2DTranspose, MaxPooling2D, concatenate, Input, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
from tensorflow.keras import backend as K
from tensorflow.keras.metrics import Precision, Recall, AUC, Accuracy
K.set_image_data_format('channels_last')
smooth = 1
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def dice_loss(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (1 -(2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth))
pr_metric = AUC(curve='PR', num_thresholds=10, name = 'pr_auc')
roc_metric = AUC(name = 'auc')
METRICS = [dice_coef,
Precision(name='precision'),
Recall(name='recall'),
pr_metric, roc_metric
]
########## Initialization of Parameters #######################
image_row = 128
image_col = 128
image_depth = 2
def denseunet():
inputs = Input((image_row, image_col, image_depth))
conv11 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conc11 = concatenate([inputs, conv11], axis=3)
conv12 = Conv2D(32, (3, 3), activation='relu', padding='same')(conc11)
conc12 = concatenate([inputs, conv12], axis=3)
pool1 = MaxPooling2D(pool_size=(2, 2))(conc12)
conv21 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conc21 = concatenate([pool1, conv21], axis=3)
conv22 = Conv2D(64, (3, 3), activation='relu', padding='same')(conc21)
conc22 = concatenate([pool1, conv22], axis=3)
pool2 = MaxPooling2D(pool_size=(2, 2))(conc22)
conv31 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conc31 = concatenate([pool2, conv31], axis=3)
conv32 = Conv2D(128, (3, 3), activation='relu', padding='same')(conc31)
conc32 = concatenate([pool2, conv32], axis=3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conc32)
conv41 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conc41 = concatenate([pool3, conv41], axis=3)
conv42 = Conv2D(256, (3, 3), activation='relu', padding='same')(conc41)
conc42 = concatenate([pool3, conv42], axis=3)
drop4 = Dropout(0.6)(conv42)
pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
conv51 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conc51 = concatenate([pool4, conv51], axis=3)
conv52 = Conv2D(512, (3, 3), activation='relu', padding='same')(conc51)
conc52 = concatenate([pool4, conv52], axis=3)
drop5 = Dropout(0.6)(conc52)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(drop5), conc42], axis=3)
conv61 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conc61 = concatenate([up6, conv61], axis=3)
conv62 = Conv2D(256, (3, 3), activation='relu', padding='same')(conc61)
conc62 = concatenate([up6, conv62], axis=3)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conc62), conv32], axis=3)
conv71 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conc71 = concatenate([up7, conv71], axis=3)
conv72 = Conv2D(128, (3, 3), activation='relu', padding='same')(conc71)
conc72 = concatenate([up7, conv72], axis=3)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conc72), conv22], axis=3)
conv81 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conc81 = concatenate([up8, conv81], axis=3)
conv82 = Conv2D(64, (3, 3), activation='relu', padding='same')(conc81)
conc82 = concatenate([up8, conv82], axis=3)
up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conc82), conv12], axis=3)
conv91 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conc91 = concatenate([up9, conv91], axis=3)
conv92 = Conv2D(32, (3, 3), activation='relu', padding='same')(conc91)
conc92 = concatenate([up9, conv92], axis=3)
conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conc92)
model = Model(inputs=[inputs], outputs=[conv10])
#model.summary()
model.compile(optimizer = Adam(learning_rate=1e-5),
loss = dice_loss, metrics=METRICS)
pretrained_weights = None
if(pretrained_weights):
model.load_weights(pretrained_weights)
return model