-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
182 lines (144 loc) · 9.95 KB
/
models.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-
"""
//////////////////////////////////////////////////////////////////////////////////////////
// Original author: Aritz Lizoain
// Github: https://github.com/aritzLizoain
// My personal website: https://aritzlizoain.github.io/
// Description: CNN Image Segmentation
// Copyright 2020, Aritz Lizoain.
// License: MIT License
//////////////////////////////////////////////////////////////////////////////////////////
ARCHITECTURE: U-Net
Original: https://arxiv.org/pdf/1505.04597.pdf
"""
import numpy as np
import os
import matplotlib.pyplot as plt
import numpy as np
from keras.models import *
from keras.layers import *
from keras.optimizers import *
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as keras
import keras.losses
from keras import regularizers #fixing overfitting with L2 regularization
import keras.backend as K
#############################################################################
#Imbalanced dataset --> weighted loss function cross entropy is needed
#Images too biased towards the first class (background ~95%)
#WEIGHTED LOSS FUNCTION CROSS ENTROPY
def weighted_categorical_crossentropy(weights= [1.,1.,1.,1.]):
print('The used loss function is: weighted categorical crossentropy')
def wcce(y_true, y_pred):
Kweights = K.constant(weights)
if not K.is_tensor(y_pred): y_pred = K.constant(y_pred)
y_true = K.cast(y_true, y_pred.dtype)
return K.categorical_crossentropy(y_true, y_pred) * K.sum(y_true * Kweights, axis=-1)
return wcce
#----------------------------------------------------------------------------
"""
UNET
It takes images (n_img, h, w, 3(rgb)) and masks (n_img, h, w, n_classes) for training
Output has shape (n_img, h, w, n_classes)
Comments are prepared to change number of layers
"""
def unet(pretrained_weights = None, input_size = (256,256,3), weights= [1.,1.,1.,1.],\
activation='relu', dropout=0, loss='categorical_crossentropy', optimizer='adam',\
dilation_rate=(1,1), reg=0.01):
inputs = Input(input_size)
s = Lambda(lambda x: x / 255) (inputs)
#CONTRACTIVE Path (ENCODER)
# cm3 = Conv2D(2, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (s)
# cm3 = Dropout(dropout) (cm2)
# cm3 = Conv2D(2, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (cm2)
# pm3 = MaxPooling2D((2, 2)) (cm2)
# pm3 = BatchNormalization()(pm2)
# cm2 = Conv2D(4, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (s)
# cm2 = Dropout(dropout) (cm2)
# cm2 = Conv2D(4, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (cm2)
# pm2 = MaxPooling2D((2, 2)) (cm2)
# pm2 = BatchNormalization()(pm2)
# cm1 = Conv2D(8, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (s)
# cm1 = Dropout(dropout) (cm1)
# cm1 = Conv2D(8, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (cm1)
# pm1 = MaxPooling2D((2, 2)) (cm1)
# pm1 = BatchNormalization()(pm1)
# c0 = Conv2D(16, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (pm1)
# c0 = Dropout(dropout) (c0)
# c0 = Conv2D(16, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (c0)
# p0 = MaxPooling2D((2, 2)) (c0)
# p0 = BatchNormalization()(p0)
c1 = Conv2D(32, 3 , activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (s)
c1 = Dropout(dropout) (c1)
c1 = Conv2D(32, 3 , activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c1)
p1 = MaxPooling2D((2, 2)) (c1)
# p1 = BatchNormalization()(p1)
c2 = Conv2D(64, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (p1)
c2 = Dropout(dropout) (c2)
c2 = Conv2D(64, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c2)
p2 = MaxPooling2D((2, 2)) (c2)
# p2 = BatchNormalization()(p2)
c3 = Conv2D(128, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (p2)
c3 = Dropout(dropout) (c3)
c3 = Conv2D(128, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c3)
p3 = MaxPooling2D((2, 2)) (c3)
# p3 = BatchNormalization()(p3)
c4 = Conv2D(256, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (p3)
c4 = Dropout(dropout) (c4)
c4 = Conv2D(256, 3, activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c4)
p4 = MaxPooling2D(pool_size=(2, 2)) (c4)
# p4 = BatchNormalization()(p4)
c5 = Conv2D(512, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (p4)
c5 = Dropout(dropout) (c5)
c5 = Conv2D(512, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c5)
#EXPANSIVE Path (DECODER)
u6 = Conv2DTranspose(256, 2, strides=(2, 2), padding='same') (c5)
u6 = concatenate([u6, c4])
c6 = Conv2D(256, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (u6)
c6 = Dropout(dropout) (c6)
c6 = Conv2D(256, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c6)
u7 = Conv2DTranspose(128, 2, strides=(2, 2), padding='same') (c6)
u7 = concatenate([u7, c3])
c7 = Conv2D(128, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (u7)
c7 = Dropout(dropout) (c7)
c7 = Conv2D(128, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c7)
u8 = Conv2DTranspose(64, 2, strides=(2, 2), padding='same') (c7)
u8 = concatenate([u8, c2])
c8 = Conv2D(64, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (u8)
c8 = Dropout(dropout) (c8)
c8 = Conv2D(64, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c8)
u9 = Conv2DTranspose(32, 2, strides=(2, 2), padding='same') (c8)
u9 = concatenate([u9, c1], axis=3)
c9 = Conv2D(32, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (u9)
c9 = Dropout(dropout) (c9)
c9 = Conv2D(32, 3, activation=activation, dilation_rate=dilation_rate, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(reg)) (c9)
# u10 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same') (c9)
# u10 = concatenate([u10, c0], axis=3)
# c10 = Conv2D(16, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (u10)
# c10 = Dropout(dropout) (c10)
# c10 = Conv2D(16, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (c10)
# u11 = Conv2DTranspose(8, (2, 2), strides=(2, 2), padding='same') (c10)
# u11 = concatenate([u11, cm1], axis=3)
# c11 = Conv2D(8, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (u11)
# c11 = Dropout(dropout) (c11)
# c11 = Conv2D(8, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (c11)
# u12 = Conv2DTranspose(4, (2, 2), strides=(2, 2), padding='same') (c10)
# u12 = concatenate([u12, cm2], axis=3)
# c12 = Conv2D(4, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (u12)
# c12 = Dropout(dropout) (c12)
# c12 = Conv2D(4, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (c12)
# u13 = Conv2DTranspose(2, (2, 2), strides=(2, 2), padding='same') (c11)
# u13 = concatenate([u12, cm2], axis=3)
# c13 = Conv2D(2, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (u12)
# c13 = Dropout(dropout) (c12)
# c13 = Conv2D(2, (3, 3), activation=activation, kernel_initializer='he_normal', padding='same', kernel_regularizer=regularizers.l2(0.01)) (c12)
#softmax as activaition in the last layer
outputs = Conv2D(4, 1, activation='softmax') (c9)
model = Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer=optimizer, loss=loss,\
metrics = ['accuracy'])
#model.summary()
if(pretrained_weights):
print('Using {0} pretrained weights'.format(pretrained_weights))
model.load_weights(pretrained_weights)
return model