Spaces:
Runtime error
Runtime error
Commit
·
c691f41
1
Parent(s):
efa4969
Upload simple_unet_model.py
Browse files- simple_unet_model.py +68 -0
simple_unet_model.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from keras.models import Model
|
| 2 |
+
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda
|
| 3 |
+
|
| 4 |
+
################################################################
|
| 5 |
+
def simple_unet_model(IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS):
|
| 6 |
+
#Build the model
|
| 7 |
+
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
|
| 8 |
+
#s = Lambda(lambda x: x / 255)(inputs) #No need for this if we normalize our inputs beforehand
|
| 9 |
+
s = inputs
|
| 10 |
+
|
| 11 |
+
#Contraction path
|
| 12 |
+
c1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s)
|
| 13 |
+
c1 = Dropout(0.1)(c1)
|
| 14 |
+
c1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
|
| 15 |
+
p1 = MaxPooling2D((2, 2))(c1)
|
| 16 |
+
|
| 17 |
+
c2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
|
| 18 |
+
c2 = Dropout(0.1)(c2)
|
| 19 |
+
c2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
|
| 20 |
+
p2 = MaxPooling2D((2, 2))(c2)
|
| 21 |
+
|
| 22 |
+
c3 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)
|
| 23 |
+
c3 = Dropout(0.2)(c3)
|
| 24 |
+
c3 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
|
| 25 |
+
p3 = MaxPooling2D((2, 2))(c3)
|
| 26 |
+
|
| 27 |
+
c4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)
|
| 28 |
+
c4 = Dropout(0.2)(c4)
|
| 29 |
+
c4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c4)
|
| 30 |
+
p4 = MaxPooling2D(pool_size=(2, 2))(c4)
|
| 31 |
+
|
| 32 |
+
c5 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p4)
|
| 33 |
+
c5 = Dropout(0.3)(c5)
|
| 34 |
+
c5 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c5)
|
| 35 |
+
|
| 36 |
+
#Expansive path
|
| 37 |
+
u6 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(c5)
|
| 38 |
+
u6 = concatenate([u6, c4])
|
| 39 |
+
c6 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u6)
|
| 40 |
+
c6 = Dropout(0.2)(c6)
|
| 41 |
+
c6 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c6)
|
| 42 |
+
|
| 43 |
+
u7 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(c6)
|
| 44 |
+
u7 = concatenate([u7, c3])
|
| 45 |
+
c7 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u7)
|
| 46 |
+
c7 = Dropout(0.2)(c7)
|
| 47 |
+
c7 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c7)
|
| 48 |
+
|
| 49 |
+
u8 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(c7)
|
| 50 |
+
u8 = concatenate([u8, c2])
|
| 51 |
+
c8 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u8)
|
| 52 |
+
c8 = Dropout(0.1)(c8)
|
| 53 |
+
c8 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c8)
|
| 54 |
+
|
| 55 |
+
u9 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c8)
|
| 56 |
+
u9 = concatenate([u9, c1], axis=3)
|
| 57 |
+
c9 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u9)
|
| 58 |
+
c9 = Dropout(0.1)(c9)
|
| 59 |
+
c9 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c9)
|
| 60 |
+
|
| 61 |
+
outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)
|
| 62 |
+
|
| 63 |
+
model = Model(inputs=[inputs], outputs=[outputs])
|
| 64 |
+
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
| 65 |
+
model.summary()
|
| 66 |
+
|
| 67 |
+
return model
|
| 68 |
+
|