9951 explained code solutions for 126 technologies


python-kerasHow to use Python and Keras to implement a U-Net architecture?


Using Python and Keras, a U-Net architecture can be implemented in the following steps:

  1. Import the necessary packages:
import keras
import tensorflow as tf
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, Dropout, UpSampling2D, concatenate
  1. Create the input layer:
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
  1. Create the contracting path:
c1 = Conv2D(16, (3, 3), activation='relu', padding='same') (inputs)
c1 = Dropout(0.1) (c1)
c1 = Conv2D(16, (3, 3), activation='relu', padding='same') (c1)
p1 = MaxPooling2D((2, 2)) (c1)

c2 = Conv2D(32, (3, 3), activation='relu', padding='same') (p1)
c2 = Dropout(0.1) (c2)
c2 = Conv2D(32, (3, 3), activation='relu', padding='same') (c2)
p2 = MaxPooling2D((2, 2)) (c2)

c3 = Conv2D(64, (3, 3), activation='relu', padding='same') (p2)
c3 = Dropout(0.2) (c3)
c3 = Conv2D(64, (3, 3), activation='relu', padding='same') (c3)
p3 = MaxPooling2D((2, 2)) (c3)

c4 = Conv2D(128, (3, 3), activation='relu', padding='same') (p3)
c4 = Dropout(0.2) (c4)
c4 = Conv2D(128, (3, 3), activation='relu', padding='same') (c4)
p4 = MaxPooling2D(pool_size=(2, 2)) (c4)
  1. Create the expansive path:
u5 = Conv2D(128, (3, 3), activation='relu', padding='same') (p4)
u5 = Dropout(0.2) (u5)
u5 = Conv2D(128, (3, 3), activation='relu', padding='same') (u5)

u6 = Conv2D(64, (3, 3), activation='relu', padding='same') (u5)
u6 = Dropout(0.2) (u6)
u6 = Conv2D(64, (3, 3), activation='relu', padding='same') (u6)

u7 = Conv2D(32, (3, 3), activation='relu', padding='same') (u6)
u7 = Dropout(0.1) (u7)
u7 = Conv2D(32, (3, 3), activation='relu', padding='same') (u7)

u8 = Conv2D(16, (3, 3), activation='relu', padding='same') (u7)
u8 = Dropout(0.1) (u8)
u8 = Conv2D(16, (3, 3), activation='relu', padding='same') (u8)
  1. Create the output layer:
outputs = Conv2D(1, (1, 1), activation='sigmoid') (u8)
  1. Create the model:
model = Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[dice_coef])
  1. Train the model:
model.fit(X_train, y_train, validation_data=(X_val, y_val), batch_size=16, epochs=50)

Helpful links

Edit this code on GitHub